How to check a not-defined variable in JavaScript

后端 未结 14 744
陌清茗
陌清茗 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

    Just do something like below:

    function isNotDefined(value) {
      return typeof value === "undefined";
    }
    

    and call it like:

    isNotDefined(undefined); //return true
    isNotDefined('Alireza'); //return false
    

提交回复
热议问题