Consider the following cases:
console.log({a: 1}.a);
console.log({a: 1}['a']);
They both output 1
, what is the difference between them? Which one should be used when outputting the value 1?
Consider the following cases:
console.log({a: 1}.a);
console.log({a: 1}['a']);
They both output 1
, what is the difference between them? Which one should be used when outputting the value 1?
With the second syntax you can have spaces, operators, variables and operations like:
const keyPrefix = 'a';
console.log({'a+ 1': 1}[keyPrefix + '+ ' + (3 - 2)]);
That's not possible with the first syntax. "Which one should be used when outputting the value 1?" You have to use the second syntax if the first is not possible. In all other cases it's opinion based. They have same behavior. {a: 1}.a
is syntax sugar for {a: 1}['a']
.