Javascript Objects

In javascript, everything is an object. A person can have multiple things. It has a name, address and email, and phone number. These are the things you need to sign up for a website. We can put everything within an object. Throughout the article, you will see these types of use cases what you use daily, and how behind-the-scenes things are happening.

An object is a key value pair wrapped around curly bracelets.

const someObject = {
  key1: "value1",
  key2: "value2",
  key3: "value3",
};

Here is an object called someObject with 3 key-value pairs. It can have multiple key-value pairs.

Now we will see how to write a person with javascript object.

const aPerson = {
  name: "John Doe",
  email: "john@doe.com",
  mob: +123568738993,
  address: "New Yok",
};

In this aPerson object, it has 4 property name, email, mob, and addresse.

In javascript, we can access each of the key through dot notation.

const aPerson = {
  name: "John Doe",
  email: "john@doe.com",
  mob: +123568738993,
  address: "New Yok",
};

console.log(aPerson);

console.log(aPerson.name);
console.log(aPerson.email);
console.log(aPerson.mob);
console.log(aPerson.address);

The output will be

{
  name: 'John Doe',
  email: 'john@doe.com',
  mob: 123568738993,
  address: 'New Yok'
}
John Doe
john@doe.com
123568738993
New Yok

So now you know what a javascript is. Now we will write a dummy function to mock the functionality of signup and login. It will show you how to work with functions.

So whenever you open an account on Google, Facebook, Instagram, or any other website you need to create an account. For that, you need to sign up for an account. Here I am giving you a solution. But try to solve it with the instruction I am giving now.

First declare 3 variable, name, email, and password. These are the 3 inputs that you put in the UI for signup. To understand the concept we are simplifying it. If you understand this concept you understand almost more than 80% of a signup process. So let’s go. Define these 3 variables and then define a function called Signup which will take these 3 variables as input and return an object which will contain two key data and isSignedUp.

const name = "John Doe";
const email = "john@gmail.com";
const password = "password";

const Signup = () => {
  //this function will take the 3 input from ui and then check
  //wheather the input are not null or undefined and the send
  //everything to the backend to process it through api request
  //and then if everything is alright you will return isSignedUp true
  //or false
  const data = {
    name: name,
    email: email,
    password: password,
  };
  const isSignedUp = true;
  return {
    data: data,
    isSignedUp: isSignedUp,
  };
};

const signUpData = Signup();
console.log(signUpData);

The output will be

{
  data: { name: 'John Doe', email: 'john@gmail.com', password: 'password' },
  isSignedUp: true
}

Within the Signup function, you have seen some normal sentence which is written after //. This is the way you will write some comments to describe what the function is doing. It is recommended always use comments whenever it is possible, It will make your help others as well as you to understand the code. Sometimes after a month, you want to look back into the code. It is tough to understand the logic of codes if you come later. That is why always write in comments what you are trying to achieve with this code.

So in the above function, the function is returning an object. This concept is very essential to understand. A function can return anything. It may an object, array, boolean whatever may be it is. It can return a function as well too.

So in this article, you have learned Objects and how to use them with functions. In the next article, we will see Javascript Array and use of it with function.

Leave a Comment