25 May, 2009
0 Comments
0 categories
There is a difference between a varaible declarde as:
var test;
and a variable declared as:
var test = null;
The first variable has a value of undefined whereas the second variable has a value of null;
Code example:
<script type="text/javascript"> var test; if(test == null) { alert("The variable [test] equals [undefined] or [null]"); } var test1 = null; if(test1 == null) { alert("The variable [test1] equals [undefined] or [null]"); } var test2 = null; if(test2 === null) { alert("The variable [test2] equals [null] and not [undefined], because we use the operator [===] and not [==]"); } var test3; if(test3 === undefined) { alert("The variable [test3] equals [undefined] and not [null], because we use the operator [===] and not [==]"); } </script>
Tags: Javascript
One Reply to “undefined vs null in javascript”