0 votes
in JavaScript by
How do you add a key value pair in javascript?

1 Answer

0 votes
by

There are two possible solutions to add new properties to an object. Let's take a simple object to explain these solutions.

var object = {
  key1: value1,
  key2: value2,
};
  1. Using dot notation: This solution is useful when you know the name of the property
object.key3 = "value3";
  1. Using square bracket notation: This solution is useful when the name of the property is dynamically determined.
obj["key3"] = "value3";
...