0

I am currently trying to develop a deeper understanding of JavaScript type coercion. While playing around with the node REPL environment, I encountered the statements []+{} and let x = []+{}. After executing the second statement, the variable x holds the value undefined, although I expected that the expression evaluates to '[object Object]' in both statements.

What causes this difference?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    I'm not able to replicate this. In my Node REPL, in my browser console, etc. the value of `x` is always `'[object Object]'`. How are you observing the value? – David Jul 17 '23 at 16:30
  • 2
    What happens if you type `x` after that `let` declaration? – Pointy Jul 17 '23 at 16:30
  • `let y = 3` results in `undefined` as well. You have to type the variable to get it's value `y` results in `3`. I guess that's because statements return nothing and `let ...` is a statement – Konrad Jul 17 '23 at 16:34

1 Answers1

1

That's because let statement returns undefined, not variable value. To retrieve variable value, you have to type the variable name.

let x = [] + {};
// undefined
x;
// "[object Object]"
MfyDev
  • 439
  • 1
  • 12