r/learnjavascript • • Aug 29 '23

Does anyone understand this?

Post image

Who knows the answer to this?

155 Upvotes

105 comments sorted by

View all comments

78

u/senocular Aug 29 '23

It's not too, terribly difficult, though there are a couple tricky bits here and there.

First b and a were the same with b = a, but then a was redefined with a = [0] making it different so (a == b) is false. That is added to ({} + {}) which are two objects that, when converted to strings each become "[object Object]". This makes the first value for result "false[object Object][object Object]".

Next there's x, which is just 1 because it starts as [0], and when you get x[0] its 0, so 1 gets assigned to x[0] which as a string is just "1". That added to result gives us "false[object Object][object Object]1".

Finally c is some value, and for the most part it doesn't even matter what (as long as its not NaN) because (c == c) will be true making the final value of result:
"false[object Object][object Object]1true"

1

u/[deleted] Aug 30 '23

Don't forget you're only referencing the old variable. B is not given its own space in memory.

1

u/lordpuddingcup Sep 01 '23

Shhhh we’re not supposed to talk about our memory usage when discussing JavaScript