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

前端 未结 30 1481
耶瑟儿~
耶瑟儿~ 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 03:56

    Best way:

    if(typeof variable==='undefined' || variable===null) {
    
    /* do your stuff */
    }
    
    0 讨论(0)
  • 2020-11-22 03:57

    I know I'm 10 years late. But I'll leave my answer here just in case anybody needs a short method.

    Installing Lodash in your project could be useful because of the helper functions that could come in handy in these situations.

    Using ES6 modules, import would look like this:

    import isNull from 'lodash/isNull';
    
    import isUndefined from 'lodash/isUndefined';
    
    import isNil from 'lodash/isNil';
    

    Would be better if only the used functions are imported.

    Lodash's isNull checks if value is null.

    const value = null;
     
     if(isNull(value)) {
        // do something if null
     }
    

    lodash's isUndefined checks if value is undefined.

    const value = undefined;
    
    if(isUndefined(value)) {
       // do something if undefined.
    }
    

    isNil Checks if value is null OR undefined. I prefer this method over other two, because it checks both undefined and null.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-11-22 04:03

    Probably the shortest way to do this is:

    if(EmpName == null) { /* DO SOMETHING */ };
    

    Here is proof:

    function check(EmpName) {
      if(EmpName == null) { return true; };
      return false;
    }
    
    var log = (t,a) => console.log(`${t} -> ${check(a)}`);
    
    log('null', null);
    log('undefined', undefined);
    log('NaN', NaN);
    log('""', "");
    log('{}', {});
    log('[]', []);
    log('[1]', [1]);
    log('[0]', [0]);
    log('[[]]', [[]]);
    log('true', true);
    log('false', false);
    log('"true"', "true");
    log('"false"', "false");
    log('Infinity', Infinity);
    log('-Infinity', -Infinity);
    log('1', 1);
    log('0', 0);
    log('-1', -1);
    log('"1"', "1");
    log('"0"', "0");
    log('"-1"', "-1");
    
    // "void 0" case
    console.log('---\n"true" is:', true);
    console.log('"void 0" is:', void 0);
    log(void 0,void 0); // "void 0" is "undefined" 

    And here are more details about == (source here)

    BONUS: reason why === is more clear than == (look on agc answer)

    0 讨论(0)
  • 2020-11-22 04:03

    If the variable you want to check is a global, do

    if (window.yourVarName) {
        // Your code here
    }
    

    This way to check will not throw an error even if the yourVarName variable doesn't exist.

    Example: I want to know if my browser supports History API

    if (window.history) {
        history.back();
    }
    

    How this works:

    window is an object which holds all global variables as its properties, and in JavaScript it is legal to try to access a non-existing object property. If history doesn't exist then window.history returns undefined. undefined is falsey, so code in an if(undefined){} block won't run.

    0 讨论(0)
  • 2020-11-22 04:03

    The easiest way to check is:

    if(!variable) {
      // If the variable is null or undefined then execution of code will enter here.
    }
    
    0 讨论(0)
提交回复
热议问题