3

In JavaScript, Strict Mode is supposed to throw Syntax Error if we try to define object with duplicated properties, like this:

const foo = {a: 1, a: 2};

Call me crazy, but I remember it did. Today I was in shock to find out that it doesn't at least in newest Chrome and Firefox on macOS.

function crazy() {
    'use strict';

    const badboy = {a: 1, a: 2};

    return badboy;
}

console.log(crazy().a); // prints 2

What happened?

Robo Robok
  • 21,132
  • 17
  • 68
  • 126
  • It's ES6 - try it in an ES5 strict environment. – Jack Bashford Jun 18 '19 at 21:25
  • 1
    It looks like they allowed it because of the ellipsis syntax: `{...foo, ...bar}`. I'm sad for the old-fashioned literal, but with ellipsis I agree it makes more sense to allow duplicates. – Robo Robok Jun 18 '19 at 21:29

0 Answers0