DEV Community

Bhupesh Chandra Joshi
Bhupesh Chandra Joshi

Posted on

Understanding Objects in JavaScript

The fact that "everything in JavaScript is an object" is inaccurate, Objects contain six or seven sub types, it dependent on your viewpoint. Functions are one of the sub-type of object and it behaves like object, you can add properies to the function. If we discuss the if statement and the loop( for while do while) in JavaScript, they are not objects,when you check the type, the result will be visible to you.

Objects are collection of key/value pairs. The values can be accessed as properties ,via .propName or ["propName"].

let student={
name:"Bhupesh",
isActive:true,
isBloging:true,
education:"bachelor of technology"
};

person["name"] or person.name, there are two ways to access the object.

person.name="Rohit";
console.log(person.name);

This will update the person name from Bhupesh to Rohit.

person.city = "Bageshwar"; // This will Add a new property 'Bageshwar'

console.log(city);

person.age=30;

delete person.age;

console.log(person);

Looping through object keys

Here we have utilized the for in loop and for in can access the object keys.

We will excess the value associated with the for in loop, so we will access the key and value together here.

We need to store multiple student,but Bhupesh ,you are storing the only one friend's name,where is your data, oh ,I need to create one more object to store my data and need to create an array of objects to store these objects at one place,let's do it.

let student={
name: 'Bhupesh',
isActive: true,
isBloging: true,
isStudent: true,
education: 'bachelor of technology',
city: 'Bageshwar',
};

Top comments (0)