JavaScript checking for null vs. undefined and difference between == and ===

前端 未结 8 724
忘了有多久
忘了有多久 2020-11-22 16:53
  1. How do I check a variable if it\'s null or undefined and what is the difference between the null and undefined?<

8条回答
  •  悲哀的现实
    2020-11-22 17:22

    undefined

    It means the variable is not yet intialized .

    Example :

    var x;
    if(x){ //you can check like this
       //code.
    }
    

    equals(==)

    It only check value is equals not datatype .

    Example :

    var x = true;
    var y = new Boolean(true);
    x == y ; //returns true
    

    Because it checks only value .

    Strict Equals(===)

    Checks the value and datatype should be same .

    Example :

    var x = true;
    var y = new Boolean(true);
    x===y; //returns false.
    

    Because it checks the datatype x is a primitive type and y is a boolean object .

提交回复
热议问题