Object
An Object is a collection of properties.
A property is an association between a name (or key) and a value.
For example, a person has a name, age, city, etc. These are the properties of the person.
Key | Value |
---|---|
firstName | Rahul |
lastName | Attuluri |
age | 28 |
city | Delhi |
1. Creating an Object
We can add properties into
1.1 Identifiers
A valid Identifier should follow the below rules:
- It can contain alphanumeric characters, _and$.
- It cannot start with a number.
Valid Identifiers:
Invalid Identifiers:
To use an Invalid identifier as a key, we have to specify it in quotes.
2. Accessing Object Properties
2.1 Dot Notation
Use Dot notation when the key is a valid Identifier.
2.2 Bracket Notation
2.3 Accessing Non-existent Properties
Dot Notation:
Bracket Notation:
2.4 Variable as a Key
2.5 Object Destructuring
To unpack properties from Objects, we use Object Destructuring. The variable name should match with the key of an object.
Try out creating and accessing the Object in different ways like Object destructuring, dot notation etc. in the below Code Playground.
3. Modifying Objects
3.1 Modifying Object Property
Dot Notation:
Bracket Notation:
3.2 Adding Object Property
Dot Notation:
Bracket Notation:
4. Property Value
The Value of Object Property can be
- Function
- Array
- Object
4.1 Function as a Value
Methods:
A JavaScript method is a property containing a function definition.
For example, in
, the
is an Object,
is a key and
is a Method.