1

var a = {
  key1: 1,
  key2: 2
};
console.log(a['key1', 'key2']); // print 2

Line 2 prints value 2, I don't understand why I do not have syntax error.

cнŝdk
  • 30,215
  • 7
  • 54
  • 72
S. Denis
  • 161
  • 3
  • 11
  • 6
    the good ol' `comma` operator - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator – Jaromanda X Jun 09 '17 at 10:13
  • It is just a comma expression: https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Operators/Comma_Operator – Hitmands Jun 09 '17 at 10:15

1 Answers1

1

This code works just fine, because you are using Comma operator, so it will evaluate all the operands and return the last evaluated one.

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

cнŝdk
  • 30,215
  • 7
  • 54
  • 72