How to determine if variable is undefined or null using JavaScript

JavaScript, is perhaps one of the most complex programming languages with some of the most abstract rules to name a few. However, the language, which is used predominantly to make JavaScript work is Java, has some of the most powerful tools and rules, which allows the programmer to think beyond the usual scope of programming all the way. One such rule is the usage of “==” or the equality operator.

Equality Operator is significantly used in situations, where a variable is checked with a numeric or even non-numeric value to be true or not. Unlike “=”, the values will not be stored for different variables, only the condition will be evaluated. Thus, with the equality operator, there is a simple way to check whether the variable is defined or is null.

The major difference between null and undefined is the value of any unassigned variable, which is simply used, results in the value to be undefined. Whereas, in case of null value, this is a popular special assignment value, with an ASCII value, and renders the variable it is assigned to have no value.

Example

script>

    var firstName;

    var lastName = null;

    // Try to get non existing DOM element

    var comment = document.getElementById('comment');

    

    console.log(firstName); // Print: undefined

    console.log(lastName);  // Print: null

    console.log(comment);   // Print: null

    

    console.log(typeof firstName); // Print: undefined

    console.log(typeof lastName);  // Print: object

    console.log(typeof comment);   // Print: object

    

    console.log(null == undefined)  // Print: true    

    console.log(null === undefined) // Print: false

    

    /* Since null == undefined is true, the following statements will catch both null and undefined */

    if(firstName == null){

        alert('Variable "firstName" is undefined.');

    }    

    if(lastName == null){

       alert('Variable "lastName" is null.');

    }

    

    /* Since null === undefined is false, the following statements will catch only null or undefined  */

    if(comment === undefined) {

        alert('Variable "comment" is undefined.');

    } else if(comment === null){

        alert('Variable "comment" is null.');

    }

</script>

In the program code, the first and the last name values are initiated in the first place. On observing, the value of lastName is assigned to be null. Unlike the firstName, whose value is undefined.

While in the next part, the comment is initiated in the print part, which will allow the function to print, whatever function is called. So, when console.log(firstName) is called, undefined will be the answer, followed by the lastName, which will be null.

In the next part, the value of null is equalized to that of undefined. This means, during the next “if” part, where the firstName is checked with “==” operator being null, the result will spring up multiple checks with firstName being both undefined as well as null. It will be the result for last name as well. Only the value of variable “comment” will be shown, whether the variable is null or undefined, depending on the decision statements.

View Demo

Leave a Reply

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