Backbencher.dev

JS Daily 37 - Same Key in Object

Last updated on 27 Dec, 2020

What is the output of following code?

const obj = {
  a: "Apple",
  b: "Banana",
  a: "Orange"
}
console.log(obj);
----o----

The output is:

{
  a: "Orange",
  b: "Banana"
}

In the example above, "Apple" and "Orange" are assigned to same object key(a). In such cases, last value assigned is saved to the key. That is why "Orange" is in key a.

--- ○ ---
Joby Joseph
Web Architect