0

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);

29th Sep 2023, 9:23 AM
Umer Khan
Umer Khan - avatar
10 Respuestas
+ 5
DO NOT PUT CODE IN THE TITLE SECTION. Put it in the description part or link it.
29th Sep 2023, 9:49 AM
Lisa
Lisa - avatar
+ 5
"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.
29th Sep 2023, 10:18 AM
Lisa
Lisa - avatar
+ 3
Keep in mind. Code in JavaScript is read and executed from top to bottom, following the order in which it appears in the script.
29th Sep 2023, 6:59 PM
Chris Coder
Chris Coder - avatar
+ 1
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)
29th Sep 2023, 5:09 PM
Nest Admiral
Nest Admiral - avatar
0
Probably using Z before it's defined. But var accepts hoisting so the result should result 15.
29th Sep 2023, 9:25 AM
Umer Khan
Umer Khan - avatar
0
OK Lisa
29th Sep 2023, 10:10 AM
Umer Khan
Umer Khan - avatar
0
Strict mode?
29th Sep 2023, 10:21 AM
Umer Khan
Umer Khan - avatar
0
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?
29th Sep 2023, 11:39 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
Try using alert(int(c));
29th Sep 2023, 2:47 PM
Timmothy Rain
Timmothy Rain - avatar