Object
- Non-primitive data type.
- Stores multiple collections of data.
- Simply, js Object is a collection of key-value pairs.
key: value
Syntax
const object_name = {
key_1: value_1,
key_2: value_2,
key_n: value_n
}
Object Creation
const person = {
firstName: 'Manikandan',
lastName: 'MK',
age: 24,
};
console.log(typeof person); // object
console.log(person); // { firstName: 'Manikandan', lastName: 'MK', age: 24 }
Explanation:
An object is a collection of key-value pairs.
person - object name,
firstName, lastName, age - key/name. (left side)
Manikandan, mk, 24 - value. (right side)
Key-value pairs are called properties.
ACCESSING OBJECT
1. Dot Notation
Syntax:
Object_name.key
Example:
const person = {
firstName: 'Manikandan',
lastName: 'MK',
age: 24,
};
console.log(person.firstName); // Manikandan
2. Bracket Notation
Syntax:
ObjectName["propertyName"] (property name is nothing key name)
Example:
const person = {
firstName: 'Manikandan',
lastName: 'MK',
age: 24
}
console.log(person['firstName']); // Manikandan
Nested Object
A nested Object is nothing but an Object containing another object.
Example:
const person = {
firstName: 'Manikandan',
lastName: 'MK',
age: 24,
physicalDetails: {
height: 170,
weight: 70,
},
};
console.log(person.physicalDetails); // { height: 170, weight: 70 }
console.log(person.physicalDetails.height); // 170
Remove Property from an Object
Top comments (0)