Adding elements to objects in JavaScript

You can add new elements to objects by writing them to the desired keys. Let's see how it's done. Let's have the following object:

let obj = {};

Let's add new elements to it:

obj['a'] = 1; obj['b'] = 2; obj['c'] = 3;

You can use alternative syntax:

obj.a = 1; obj.b = 2; obj.c = 3;

Check the content of the object:


Create an empty object and then fill it with values.