0 Comments

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>

One Reply to “undefined vs null in javascript”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts