Take the following example:
var obj = {};
obj.test1 = 'string'; // string
obj['test2'] = 'string'; // string
obj['test3'] = ['string']; // array
var key = 'test4';
//obj.key = 'string' // not working
var key = 'test5';
obj[key] = 'string'; // working
console.log(obj);
Why is it possible to access an object property using array notation (square brackets): obj['test2'] = 'string';
?
Why does the example at test4
using dot notation fails, but works using array notation at test5
?