0

What is the output of c? Its output is NaN. HOW???(JS)

var x=4; var y=5; var d=4+z; var z=y-x; var c=x+z+d+y; alert(c);

4th Jan 2022, 3:23 PM
Bikesh Shrestha
4 Answers
+ 5
NAN means Not-a-number. When you try to perform a mathematical operation(+,-,Ɨ,/) with undefined(here z) it cannot be coerced to a different type or a number type in this case so the result of the operation cannot be evaluated and returns NaN.
4th Jan 2022, 5:51 PM
Simba
Simba - avatar
+ 4
Because, you assigned variable z before declaring it. Do it like this var x=4; var y=5; var z=y-x; var d=4+z; var c=x+z+d+y; alert(c);
4th Jan 2022, 5:32 PM
Simba
Simba - avatar
+ 2
You have d=4+z here z in uninitialized. First take var z= .. Then var d=.. edit: Bikesh Shrestha var x=4; var y=5; var z=y-x; var d=4+z; var c=x+z+d+y; alert(c);
4th Jan 2022, 5:31 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
No no the question asked was like this only. So i want to know how it becomes NaN. Thank you so much for helping me out in this JS question.
4th Jan 2022, 5:39 PM
Bikesh Shrestha