Home  >  Article  >  Web Front-end  >  How to use JS to determine whether a variable exists

How to use JS to determine whether a variable exists

php中世界最好的语言
php中世界最好的语言Original
2018-05-29 10:00:492771browse

This time I will show you how to use JS to determine whether a variable exists, and what are the precautions for using JS to determine whether a variable exists. The following is a practical case, let's take a look.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
//http://www.jb51.net/article/67551.htm
//判断变量i是否存在 typeof(i)=="undefined"
<script>
  /*---------------------------判断函数是否存在-------------------------------*/
  function isExitsFunction(funcName) {
    try {
      if (typeof(eval(funcName)) == "function") {
        return true;
        //  funcName();
      }
    } catch (e) {
      console.log(eval(funcName) + "+++++++++++++++++我异常了!!!!!!!!");
    }
    return false;
  }
  /*--------------------------------判断是否存在指定变量 -----------------------------------------*/
  function isExitsParamsVariable(variableName) {
    try {
      console.log("variableName.length===" + variableName.length);
      if (variableName.length == 0) {
        console.log(variableName + "===value has no params");//"":length为0
        return false;
      } else {
        console.log(variableName + "======value has params");//0:length为undefined
        return true;
      }
    } catch (e) {
      console.log(variableName + "----我异常了!!!!!!!!");//null,undefined,未赋值的a
    }
    return false;//null,undefined,未赋值的a
  }
  /*---------------------------------判断是否undefined--------------------------------*/
  function isExitsVariable(variableName) {
    console.log("typeof variableName====" + typeof(variableName));
    try {
      if (typeof(variableName) == "undefined") {
        console.log(variableName + "===value is undefined");//undefined,未赋值的a
        return false;
      } else {
        console.log(variableName + "=======value is true");//null,0,""
        return true;
      }
    } catch (e) {
      console.log(variableName + "-------我异常了........");
    }
    return false;
  }
  /*-------------------------------------------------测试数据---------------------------------------------*/
  var a;//声明未初始化,没有长度
  console.log("isExitsParamsVariable(a)" + isExitsParamsVariable(a));
  console.log("isExitsVariable(a)" + isExitsVariable(a));
  console.log("--------------------------------------------------")
  var b = undefined;//没有长度
  console.log("isExitsParamsVariable(b)===" + isExitsParamsVariable(b));
  console.log("isExitsVariable(b)===" + isExitsVariable(b));
  console.log("--------------------------------------------------")
  var c = null;//没有长度
  console.log("isExitsParamsVariable(c)===" + isExitsParamsVariable(c));
  console.log("isExitsVariable(c)===" + isExitsVariable(c));
  console.log("--------------------------------------------------")
  var d = 0;//长度undefined
  console.log("isExitsParamsVariable(d)===" + isExitsParamsVariable(d));
  console.log("isExitsVariable(d)===" + isExitsVariable(d));
  console.log("--------------------------------------------------")
  var e = "";//长度为0
  console.log("isExitsParamsVariable(e)====" + isExitsParamsVariable(e));
  console.log("isExitsVariable(e)===" + isExitsVariable(e));
  console.log("--------------------------------------------------")
  /*未定义声明 f 则log会报错:Uncaught ReferenceError: f is not defined ,不会执行两个判断方法*/
  console.log("isExitsParamsVariable(f)====" + isExitsParamsVariable(f));//f:undefined
  console.log("isExitsVariable(f)===" + isExitsVariable(f));
</script>
</body>
</html>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to implement a custom multi-select event in WeChat applet


When select is not used How to implement the drop-down box function under vue

The above is the detailed content of How to use JS to determine whether a variable exists. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn