Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (6.1k points)

This is my object literal:

var obj = {key1: value1, key2: value2};

Can you tell me how to add key3 with value3 to the object?

1 Answer

0 votes
by (11.7k points)

It can be done in two ways:

var obj = {

    key1: value1,

    key2: value2

};


Dot notation:

obj.key3 = "value3";

Square bracket notation:

obj["key3"] = "value3";

When the name of the property is known, the first form is used. We use a second form when the name of the property is dynamically determined.

var getProperty = function (propertyName) {

    return obj[propertyName];

};

getProperty("key1");

getProperty("key2");

getProperty("key3");

We can construct a real JavaScript array either by using:

The Array literal notation:

var arr = [];

The Array constructor notation:

var arr = new Array();

If you want to become an expert in web technology, you can check out the latest courses provided by Intellipaat from here.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...