Objects in Javascript

Ratan Deep Singh
2 min readNov 22, 2020

Hello again everybody,
In this blog, we are going to learn about JavaScript objects, its creation and the way to access the value inside objects

Objects :
Objects are composite data type that usually takes up the value in the form of key:value pair.

#Object1
  1. Here, we have declared an object obj (it can be any name), the value is written inside the curly braces {} in the form of key:value pair.
  2. In this example, the keys are name and age and the values are “Ratan” and 25 respectively, where “Ratan” is a string and 25 is a number.
  3. The value in key:value pair can also take up composite data type such as arrays and other objects (object inside an object) too.
    For example:
#Object2

In this example, the key name has another object as a value, which in turn has multiple key-value pair inside it.
And, key favColor has an array as a value.

How to access the values inside objects:

Lets get into a little bit of coding on how to access the values inside these objects.
In #Object1, the value “Ratan” and 25, can be accessed as below:

Accessing value in #Object1

As in above example, we can access name using dot notation (obj.name) and also square braces (obj[“name”]) too.

Note: Don’t forget to use double quotes while accessing objects using square braces.

In #Object2, the value “Ratan” and “Black” can be accessed as below:

Accessing values in #Object2

In line 88, dot notation is used to access the value “Ratan” and in line 90, square braces are used to access the value “Black”.

Note: The value “Black” is in the first index of the favColor key.

Happy learning!
Ratan
Do comment and let me know if i could make my blog better.

--

--