How can I determine if a variable is 'undefined' or 'null'?

前端 未结 30 1880
耶瑟儿~
耶瑟儿~ 2020-11-22 03:30

How do I determine if variable is undefined or null?

My code is as follows:

var EmpN         


        
30条回答
  •  不要未来只要你来
    2020-11-22 04:00

    Let's look at this,

    1.  

      let apple; // Only declare the variable as apple
      alert(apple); // undefined
      

      In the above, the variable is only declared as apple. In this case, if we call method alert it will display undefined.

    2.  

         let apple = null; /* Declare the variable as apple and initialized but the value is null */
         alert(apple); // null
      

    In the second one it displays null, because variable of apple value is null.

    So you can check whether a value is undefined or null.

    if(apple !== undefined || apple !== null) {
        // Can use variable without any error
    }
    

提交回复
热议问题