Duplicate Object Literal Properties

ECMAScript 5 strict mode introduced a check for duplicate object literal properties that would throw an error if a duplicate was found. For example, this code was problematic:

  1. "use strict";
  2. var person = {
  3. name: "Nicholas",
  4. name: "Greg" // syntax error in ES5 strict mode
  5. };

When running in ECMAScript 5 strict mode, the second name property causes a syntax error. But in ECMAScript 6, the duplicate property check was removed. Both strict and nonstrict mode code no longer check for duplicate properties. Instead, the last property of the given name becomes the property’s actual value, as shown here:

  1. "use strict";
  2. var person = {
  3. name: "Nicholas",
  4. name: "Greg" // no error in ES6 strict mode
  5. };
  6. console.log(person.name); // "Greg"

In this example, the value of person.name is "Greg" because that’s the last value assigned to the property.