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

前端 未结 24 1918
悲&欢浪女
悲&欢浪女 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:14

    Best way to compare undefined or null or 0 with ES5 and ES6 standards

     if ((Boolean(some_variable_1) && Boolean(some_variable_2)) === false) {
        // do something
        }
    

提交回复
热议问题