Add Key/Value Pair to an Object

 Add Key/Value Pair to an Object 

In this example, you will learn to write a JavaScript program that will add a key/value pair to an object.

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

JavaScript Objects


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



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



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;.

Post a Comment

Previous Post Next Post