How to check for an undefined or null variable in JavaScript?

前端 未结 24 1930
悲&欢浪女
悲&欢浪女 2020-11-22 15:55

We are frequently using the following code pattern in our JavaScript code

if (typeof(some_variable) != \'undefined\' && some_variable != null)
{
             


        
相关标签:
24条回答
  • 2020-11-22 16:20

    whatever yyy is undefined or null, it will return true

    if (typeof yyy == 'undefined' || !yyy) {
        console.log('yes');
    } else {
        console.log('no');
    }
    

    yes

    if (!(typeof yyy == 'undefined' || !yyy)) {
        console.log('yes');
    } else {
        console.log('no');
    }
    

    no

    0 讨论(0)
  • 2020-11-22 16:23

    I have done this using this method

    save the id in some variable

    var someVariable = document.getElementById("someId");
    

    then use if condition

    if(someVariable === ""){
     //logic
    } else if(someVariable !== ""){
     //logic
    }
    
    0 讨论(0)
  • 2020-11-22 16:26

    here's another way using the Array includes() method:

    [undefined, null].includes(value)
    
    0 讨论(0)
  • 2020-11-22 16:29

    As mentioned in one of the answers, you can be in luck if you are talking about a variable that has a global scope. As you might know, the variables that you define globally tend to get added to the windows object. You can take advantage of this fact so lets say you are accessing a variable called bleh, just use the double inverted operator (!!)

    !!window['bleh'];
    

    This would return a false while bleh has not been declared AND assigned a value.

    0 讨论(0)
  • 2020-11-22 16:31

    In newer JavaScript standards like ES5 and ES6 you can just say

    > Boolean(0) //false
    > Boolean(null)  //false
    > Boolean(undefined) //false
    

    all return false, which is similar to Python's check of empty variables. So if you want to write conditional logic around a variable, just say

    if (Boolean(myvar)){
       // Do something
    }
    

    here "null" or "empty string" or "undefined" will be handled efficiently.

    0 讨论(0)
  • 2020-11-22 16:32

    You have to differentiate between cases:

    1. Variables can be undefined or undeclared. You'll get an error if you access an undeclared variable in any context other than typeof.
    if(typeof someUndeclaredVar == whatever) // works
    if(someUndeclaredVar) // throws error
    

    A variable that has been declared but not initialized is undefined.

    let foo;
    if (foo) //evaluates to false because foo === undefined
    
    1. Undefined properties , like someExistingObj.someUndefProperty. An undefined property doesn't yield an error and simply returns undefined, which, when converted to a boolean, evaluates to false. So, if you don't care about 0 and false, using if(obj.undefProp) is ok. There's a common idiom based on this fact:

      value = obj.prop || defaultValue
      

      which means "if obj has the property prop, assign it to value, otherwise assign the default value defautValue".

      Some people consider this behavior confusing, arguing that it leads to hard-to-find errors and recommend using the in operator instead

      value = ('prop' in obj) ? obj.prop : defaultValue
      
    0 讨论(0)
提交回复
热议问题