Check if variable is undefined

This question already has an answer here:

  • How to determine if variable is 'undefined' or 'null'? 24 answers

  • 未定义是js中的虚假...它看起来像bar.baz可能是你的罪魁祸首。


    You can use typeof to check if a variable is undefined . It always returns a string .

    if (typeof foo === 'undefined') {
      console.log('foo is undefined');
    }
    
    var foo = ['one', 'two', 'three'];
    
    if (typeof foo !== 'undefined') {
      // access elements
      console.log(foo[0] + ', ' + foo[1] + ', ' + foo[2]);
    }
    链接地址: http://www.djcxy.com/p/76352.html

    上一篇: 在Javascript中检查一些东西是否是空的?

    下一篇: 检查变量是否未定义