How to check a not-defined variable in JavaScript

后端 未结 14 996
陌清茗
陌清茗 2020-11-22 14:23

I wanted to check whether the variable is defined or not. For example, the following throws a not-defined error

alert( x );

How can I cat

14条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 15:01

    I've often done:

    function doSomething(variable)
    {
        var undef;
    
        if(variable === undef)
        {
             alert('Hey moron, define this bad boy.');
        }
    }
    

提交回复
热议问题