InterviewHubPRO
Section Overview
Q140

Question 140 of 521

Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

27%
JavaScript Internals (521 Qs)•Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

How do you add a key value pair in javascript

There are two possible solutions to add new properties to an object.

Let's take a simple object to explain these solutions.

javascript
  var object = {
    key1: value1,
    key2: value2,
  };
  1. Using dot notation: This solution is useful when you know the name of the property
javascript
  object.key3 = "value3";
  1. Using square bracket notation: This solution is useful when the name of the property is dynamically determined or the key's name is non-JS like "user-name"
javascript
  obj["key3"] = "value3";