6

I am learning Javascript and running a little experiment on Chrome browser console. I encountered a problem as follows.

let obj = {a:1, b:2}
let obj2 = {...obj, [b]:8}

Uncaught ReferenceError: b is not defined

This uncaught reference error is expected since I haven't defined b yet.

Then I tried obj2 = 3, I got an error

Uncaught ReferenceError: obj2 is not defined

When I tried let obj2 = 3, I got an error

Uncaught SyntaxError: Identifier 'obj2' has already been declared

I am really confused. Has obj2 been declared or not and why do I get such errors?

full-stack
  • 553
  • 5
  • 20
Cirrus
  • 105
  • 8
  • 1
    It is really weird :) It looks like since you did not succesfully initialize obj2 (due to b is not defined error), it could not be a defined object. However, as you gave a name to it, it occupies a place in the namespace. Solution would be correcting the error and re-running the code but as you are in console refresing the browser and succesfully initializing the obj2 should solve the problem. – Ziya ERKOC Jul 30 '18 at 19:06

0 Answers0