Dynamically creating keys in JavaScript associative array


Javascript's associative arrays are nothing but javascript object literals. You can add keys to these objects dynamically if the key is a string using the square brackets notation. For example,

Example

let a = {
   name: 'Ayush'
};
let key = 'age';
// Add the non existing key
a[key] = 35;
console.log(a)

Output

This will give the output −

{ name: 'Ayush', age: 35 }
raja
Published on 02-Dec-2019 06:49:25
Advertisements