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