JavaScript Program to Add Key/Value Pair to an Object

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Add Key/Value Pair to an Object Using Dot Notation

Output

{
    name: "Monica",
    age: 22,
    gender: "female",
    height: 5.4
}

In the above example, we add the new property height to theĀ person object using the dot notation . i.e. person.height = 5.4;.


Example 2: Add Key/Value Pair to an Object Using Square Bracket Notation

Output

{
    name: "Monica",
    age: 22,
    gender: "female",
    height: 5.4
}

In the above example, we add the new property height to theĀ person object using the square bracket notation [] i.e. person['height'] = 5.4;.