menu

JavaScript/Typescript - 16 Topics

DEEP DIVE INTO

JavaScript/Typescript

Topic:Object intro

menu

What is Object?

an object is a fundamental data structure that represents a collection of key-value pairs.

Example:

javascriptconst person = {
  //Key or property     value
  firstName          :  "John",
  lastName           :  "Doe",
  age                :  30,
  isStudent          :  false,
};

The text which is left side of full-colon (:) is call key or property and right side of full-colon is call value.

Objects are variables too. But objects can contain many values.

Formatting an object is very important, this give readability.

Example code without formatting:

javascriptconst person = {firstName : "John", lastName: "Doe",
 age : 30, isStudent.: false,
};

Compare the code from above code, you will see the difference. We should always format our code so that looks clean and readable to others. This will reduce error is code and searching key becomes an easy task.

How to read value from an object?

The value can be read from an object by dot(.) notation.

Example: let's read name of person from above person object. This is very easy.

javascript// Reading name of person
let name = person.name;
console.log(name) // Result; "John"

Where person is object and name is value.

Why object is important?

javascriptconst address = {
  //Key or property     value
  streetName          :  "Conductirs Dr",
  streetNumber        :  "129",
  pincode             :  15776,
  country             :  "USA",
};

Let's look to the address object. If you try to create variable for each address element, you will have to create 4 variables. Think about an object having 100 and more keys, then you will have create 100 different variables and sending all 100 variables from one page to another is tedious task to do.

Here object comes into picture, you can send all these variables at once from one page to another page.

You can keep very large amount of data under one veriable.

There is difference between JavaScript object and JSON.

info
1280 x 720 px