Dynamically creating keys in JavaScript associative array

JavascriptFront End TechnologyObject Oriented Programming

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 11:19:25
Advertisements