zoukankan      html  css  js  c++  java
  • js null,undefined判断

    var exp = null;

    if (!exp)

    如果 exp 为 undefined,或数字零,或 false,也会得到与 null 相同的结果,虽然 null 和二者不一样。注意:要同时判断 null、undefined、数字零、false 时可使用本法。


    var exp = null;
    if (!exp && typeof exp != "undefined" && exp != 0)
    {
        alert("is null");
    }
    • typeof exp != "undefined" 排除了 undefined;
    • exp != 0 排除了数字零和 false。

    更简单的正确的方法:

    var exp = null;
    if (exp === null)
    {
        alert("is null");
    }

    尽管如此,我们在 DOM 应用中,一般只需要用 (!exp) 来判断就可以了,因为 DOM 应用中,可能返回 null,可能返回 undefined,如果具体判断 null 还是 undefined 会使程序过于复杂。


  • 相关阅读:
    Python2-列表
    C#1-变量和表达式
    Python1-变量
    grep输出带有颜色设置
    ftp服务器的安装与使用
    慕课网-哒哒租车系统
    ulimit 命令
    ARP与RARP
    return 和exit
    java运行机制
  • 原文地址:https://www.cnblogs.com/henw/p/2044166.html
Copyright © 2011-2022 走看看