Anyone please tell what is the difference between undefined and null in javascript and when I should use undefined and when null.
Asked
Active
Viewed 2,076 times
-1
-
6See this: http://stackoverflow.com/questions/5101948/javascript-checking-for-null-vs-undefined-and-difference-between-and – Muhammad Omair Nov 25 '15 at 12:20
-
http://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript – Kumar Saurabh Nov 25 '15 at 12:20
2 Answers
2
undefined means a variable has been declared but has not yet been assigned. null is an assignment value. It can be assigned to a variable as a representation of no value
example.
var a;
alert(typeof(a));
var b = null;
alert(typeof(b));
Running the above script will result in the following output:
undefined object
——————————-

Nisha Salim
- 687
- 5
- 13
0
undefined
is not defined at all... means, that given variable does not exist.
null
is defined variable that has value set to null (so basically you can say that it is defined variable with undefined value)

Flash Thunder
- 11,672
- 8
- 47
- 91