zoukankan      html  css  js  c++  java
  • 判断 undefined and ( == null) and (!something) and ( == null)

    1、判断 undefined

    var a = '',
        b = ' ', 
        c = undefined ,
        d = null ,
        e = 1 ,
        f = false ,
        g = 0 ,
        h = "0";
        
        if(a === void 0){
          console.log("come in");
        }else{
          console.log("not in here");
        }
        
        //a  print: not in here
        //b  print: not in here
        //c  print: come in
        //d  print: not in here
        //e  print: not in here
        //f  print: not in here
        //g  print: not in here
        //h  print: not in here
     

    2、判断 (!something)

    var exp = null ,
        und = undefined ,
        no = '' ,
        space = ' ',
        fal = false ,
        zero = 0;
        if (!exp)
        {
            console.log("is null or nudefined or '' or false or 0");
        }else{
            console.log("is something");
        }
        
        //exp    print: is null or nudefined or '' or false or 0
        //und    print: is null or nudefined or '' or false or 0
        //no     print: is null or nudefined or '' or false or 0
        //space  print: is something
        //fal    print: is null or nudefined or '' or false or 0
        //zero   print: is null or nudefined or '' or false or 0

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

        javascript中以下值会被转换为false
    • false
    • undefined
    • null
    • 0
    • -0
    • NaN
    • ""

     

    3、判断 ( == null)

    var exp = null ,
        und = undefined ,
        no = '' ,
        space = ' ',
        fal = false ,
        zero = 0;
        if (zero == null)
        {
            console.log("is null");
        }else{
            console.log("here is another");
        }
        
        //exp    print: is null
        //und    print: is null
        //no     print: here is another
        //space  print: here is another
        //fal    print: here is another
        //zero   print: here is another

    判断条件为 undefined 时,也会得到与 null 相同的结果,虽然 null 和 undefined 不一样。 注意:要同时判断 null 和 undefined 时可使用本法。

     

    4、判断( == null) 方法1

    var exp = null ,
        und = undefined ,
        undStr = 'undefined' ,
        no = '' ,
        space = ' ',
        fal = false ,
        zero = 0;
        
        if (!undStr && typeof undStr != "undefined" && undStr != 0)
        {
            console.log("is null");
        }else{
            console.log("is not null")
        }
        
        //exp       print: is null
        //und       print: is not null
        //undStr    print: is not null
        //no        print: is not null
        //space     print: is not null
        //fal       print: is not null
        //zero      print: is not null

    typeof exp != "undefined" 排除了 undefined; exp != 0 排除了数字零和 false。

     

    5、判断null 方法2

    var exp = null ,
        und = undefined ,
        no = '' ,
        space = ' ',
        fal = false ,
        zero = 0;
        if (exp === null)
        {
            console.log("is null");
        }else{
            console.log("is not null")
        }
        
        //exp       print: is null
        //und       print: is not null
        //no        print: is not null
        //space     print: is not null
        //fal       print: is not null
        //zero      print: is not null

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

  • 相关阅读:
    7、NFC技术:让Android自动运行程序
    6、Android中的NFC技术
    5、NFC概述
    Delphi XE7中开发安卓程序一些有用的帮助资源
    Delphi开发安卓程序的感受
    Tomcat绿色版启动"startup.bat"一闪问题的解决方法!
    Delphi判断字符串中是否包含汉字,并返回汉字位置
    Delphi的DLL里如何实现定时器功能?
    Delphi的DLL里如何实现定时器功能?
    VS2013如何添加LIb库及头文件的步骤
  • 原文地址:https://www.cnblogs.com/xmyun/p/6727795.html
Copyright © 2011-2022 走看看