Hello Friends 👋,

Welcome To Infinitbility! ❤️

To transfer data from one file to another, or project to another we used JSON i.e we need many times to add data in JSON objects statically, and dynamically.

Today, we will see how to add elements to an object and also how many ways to do that with example.

let’s start with when we create a new object.

Create an object with key-value pair

When we create a new object we mainly use {} curly brace to create and when we want to create an object with value. we follow the below code.

let exampleObject = {
  id: 1,
  name: "infinitbility",
};

Add element in already created object statically

let’s assume we already created the object now we want to add more elements to it then we will follow the below code.

let exampleObject = {
  id: 1,
  name: "infinitbility",
};

//  Add element using key
exampleObject.domain = "infinitbility.com";
console.log("exampleObject", exampleObject);

Add element in already created object dynamically

Now, many times we have to add elements in already create objects dynamically, for dynamic we have the option below code example.

let exampleObject = {
  id: 1,
  name: "infinitbility",
};

// Add element for dynamic key
exampleObject["isActive"] = true;
console.log("exampleObject", exampleObject);

Output

javascript, add, element, object, example
Javascript, add key-value pair in json object example.

Thanks for reading…