Why this code results in NaN?
var x = 4; var y = 5; var d = 4 + z; var z = y - x; var c = x+z+d+y; alert(c);
DO NOT PUT CODE IN THE TITLE SECTION. Put it in the description part or link it.
"z" has not been defined when using it in line 3, 4 + undefinded is not a number. In strict mode, this code may not work. edit: Seems to work in strict mode.
You're right about "var" hoisting and the result should be 14 not 15. Ok so when you initialised "d", z is "undefined". Just one line above "d", z has no numerical value. From the philosophical POV, the value of d is the same thing as var d = 4 + sololearn or var d = 4 + lion It doesn't make any sense in numerical term and therefore isn't a number, so z must be initialized to a number first z = 0; var d = 4 + z; var z = y - x;
at the time "d" is declared, "z" is not defined yet, resulting in undefined, this makes any calculation on it worthless (still results in undefined)
Probably using Z before it's defined. But var accepts hoisting so the result should result 15.
Umer In 3rd statement you didn't even defined z variable and using it for sum. You have define it later. And what you even mean by strict mode?
Try using alert(int(c));