0 votes
1 view
in Web Technology by (2.3k 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
ago by (4.4k 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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...