+ 3

JS challenge question help

Hello community, I have a question about the following code (function() { let a = b = 3; })(); console.log(typeof a == "undefined"); console.log(typeof b == "number"); Why does first comparison evaluates to true? Doesnt a gets assigned value of b, which is three, because of the associativity? Instead, It looks like it is indeed undefined.

9th Dec 2020, 8:15 PM
David
David - avatar
4 Respostas
+ 2
According to MDN "a = b = 5; // same as writing a = (b = 5);" It looks as if the let keyword only applies to variable a. let a = b = 3 || let a = (b = 3) It also appears that variable b is globally scoped, while variable a is block-scoped inside of the IFIIE, hence the reason both evaluations return true. I ran the snippet on playground(web and node) and b is globally scoped (CMIIW)
9th Dec 2020, 9:35 PM
ODLNT
ODLNT - avatar
+ 2
I thought it gets assigned value of 3 and so its typeof then changes to number. Thanks for your help though.
9th Dec 2020, 9:53 PM
David
David - avatar
+ 2
I didnt do this code myself, It was created by someone and is a legit question in challenges
9th Dec 2020, 10:07 PM
David
David - avatar
0
You're welcome
9th Dec 2020, 10:05 PM
ODLNT
ODLNT - avatar