What are Objects?Key and ValueKeys are unique identifiers also known as property name and the values can be of any type [string, boolean, integer, function or objects]
- Mutable in nature
- Access using Dot notation
- Providing a flexible and dynamic way to store and manipulate the data.
- Allow to group related data and behaviors together.
- Objects can be created by using object literal {} or by constructor Object()
const abc = {
name : “mohit”,
section : “B”,
“Blood Group” : “B+”
}
const abc = new Object();
abc.name = “mohit”;
abc.section = “B”;
abc.[“Blood Group”] = “B+”
Access Object properties using Dot Notation
Very common and simple way to access the object properties
const obj = {
name : “raj”,
section : “C”,
}
console.log(obj.name)
obj.name = “Rajesh”
console.log(obj.name)
Access Object properties using Bracket Notation
They are useful when a property name contains special character or are dynamic.
console.log(obj[“name”])
const key = “section”
console.log(obj[key])
Iterating over object properties
Object is a constructor and it provides same functionality to all JavaScript objects.
• To get an array of keys from the object
console.log(Object.keys(obj))
• To get an array of values from the object
console.log(Object.values(obj))
• To get the combination of all keys and elements
console.log(Object.entries(obj))
Adding and Removing Object items
obj.number = 2584785996
delete obj.section;
console.log(Object.entries(obj))
Object with methods, prototype, inheritance and loop in objects are also there, I am not skipping them all but it would be better and comfortable if discussed them when we reach to that topic. If you want more such content then make sure to following me on all available platform
Instagram
LinkedIn
Hashnode
Thank you for reading 🤍
Written By Mohit Soni
Source: hashnode.com