How to use Dot Notation In Javascript
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.
const obj = { Organisation: "GFG" };
obj.field = "CS";
console.log(obj);
Output
{ Organisation: 'GFG', field: 'CS' }
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
- Using Object.entries() and Object.fromEntries()
Contact Us