• DSA with JS - Self Paced
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Operator
  • JS Projects
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
  • JavaScript Object Programming Examples
  • Remove blank attributes from a JavaScript Object
  • How to Perform CRUD Operations on JavaScript Object ?
  • Create a chart from JSON data using Fetch GET request (Fetch API) in JavaScript
  • How to serialize an object into a list of URL query parameters using JavaScript ?
  • How to add a property to a JavaScript object using a variable as the name?
  • How to create dynamic values and objects in JavaScript ?
  • Convert URL parameters to a JavaScript Object
  • How to get dynamic access to an object property in JavaScript ?
  • How to get the last item of JavaScript object ?
  • JavaScript Get the index of an object by its property
  • How to get all the methods of an object using JavaScript ?
  • How to remove a property from JavaScript object ?
  • How to unflatten an object with the paths for keys in JavaScript ?
  • How to change JSON String into an Object in JavaScript ?
  • How to create an object with prototype in JavaScript ?
  • How to test a string as a literal and as an object in JavaScript ?
  • How to convert a plain object into ES6 Map using JavaScript ?
  • How to add and remove properties from objects in JavaScript ?

How to add key/value pair to a JavaScript object ?

JavaScript object has key-value pair and it can be of variable length. We first need to declare an object and assign the values to that object for that there can be many methods. In this article, we are going to apply different approaches to adding a key/value pair to a JavaScript object.

These are the following methods to add a key/value pair to a JavaScript object:

Table of Content

  • Using Dot Notation
  • Using Bracket Notation
  • Using Spread operator
  • Using Objcet.assign() method
  • Using Objcet.defineProperty() method

Method 1: Using Dot Notation

In this approach, we will directly access the key of an object by “.” for assigning the value to it. It can be a repeated method as every time we need to define a different key for different values.

Example: This example shows the implementation of the above approach.

Method 2: Using Bracket Notation

In this approach, we are using Bracket to access the key and assign the value to it.

Method 3: Using Spread operator

In this approach, we are using Spread operator to assign the value to the object. we will create a new object and by using the spread operator we can add the old key and value to the new object with new key value pair.

Method 4: Using Objcet.assign() method

In this approach, we are using Objcet.assign() method to assign the key and value to the object. We are passing the object name and the field that we want to use in the object. It assign those fields to the given object.

Method 5: Using Objcet.defineProperty() method

In this approach, we are using Objcet.defineProperty() method to assign the key and value to the object. We are passing the object name and the field that we want to use in the object with some extra specification that is served by the method itself.

Please Login to comment...

  • javascript-object
  • JavaScript-Questions
  • Web Technologies
  • How to Delete Whatsapp Business Account?
  • Discord vs Zoom: Select The Efficienct One for Virtual Meetings?
  • Otter AI vs Dragon Speech Recognition: Which is the best AI Transcription Tool?
  • Google Messages To Let You Send Multiple Photos
  • 30 OOPs Interview Questions and Answers (2024)

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

How to Add a Key/Value Pair to an Object in Javascript

Topic: JavaScript / jQuery Prev | Next

Answer: Use Dot Notation or Square Bracket

You can simply use the dot notation ( . ) to add a key/value pair or a property to a JavaScript object.

Let's try out the following example to understand how it basically works:

Alternatively, you can also use the square bracket notation ( [] ) to add a key/value pair to a JavaScript object. The following example produces the same result as the previous example:

The advantage of using square bracket notation is, you can substitute the key inside the square bracket with a variable to dynamically assign a key or property name to an object.

See the tutorial on JavaScript Objects to learn more about creating and manipulating objects.

Related FAQ

Here are some more FAQ related to this topic:

  • How to check if a value is an object in JavaScript
  • How to remove a property from a JavaScript object
  • How to convert JavaScript object to JSON string

Bootstrap UI Design Templates

Is this website helpful to you? Please give us a like , or share your feedback to help us improve . Connect with us on Facebook and Twitter for the latest updates.

Interactive Tools

BMC

IMAGES

  1. The Object.assign() Method in JavaScript

    assign key to object javascript

  2. Explain Object.keys in JavaScript

    assign key to object javascript

  3. Javascript Tutorial : Object.assign() Method in JavaScript

    assign key to object javascript

  4. How to Get an Object’s Keys and Values in JavaScript

    assign key to object javascript

  5. How to Get Object Keys in JavaScript

    assign key to object javascript

  6. Add key value to object JavaScript

    assign key to object javascript

VIDEO

  1. Remove Properties From JavaScript Object (Copy) #javascript #javascriptlearning #javascriptdeveloper

  2. 04 what is JavaScript Object assign in Telugu

  3. Is Key Exist in JavaScript Object?

  4. How to use Destructuring Assignment in JavaScript

  5. Align to a Key Object (SOLVED!)

  6. Объекты (this) в JS

COMMENTS

  1. How to add key/value pair to a JavaScript object

    Dot notation. The most common and straightforward way to add a key-value pair to an object is to use the dot notation. You have probably already used this in the past, and it's sufficient in most situations you will encounter. const obj ={a:1}; obj.b=2; obj.c=3;// obj = { a: 1, b: 2, c: 3 }

  2. How to Add a Key/Value Pair to an Object in Javascript

    var myValue = "Petrol"; // Dynamically adding a key/value pair. myCar[myKey] = myValue; console.log(myCar); See the tutorial on JavaScript Objects to learn more about creating and manipulating objects. Here are some more FAQ related to this topic: