• 判断 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 会使程序过于复杂。

  • 相关阅读:
    变分自编码器(Variational Autoencoder, VAE)通俗教程
    神经网络图灵机(Neural Turing Machines, NTM)
    【Java设计模式】单例模式
    SupportV7包中 SwipeRefreshLayout 修改下拉控件的距离
    eclipse项目导入androidstudio
    LiveWriter Test
    【Android学习】XML文本的三种解析方式(通过搭建本地的Web项目提供XML文件)
    【百度地图学习-一】初始化以及TextOverlay
    【Android学习】四种布局方式
    【Android学习】数据传递三种方式
  • 原文地址:https://www.cnblogs.com/xmyun/p/6727795.html
走看看 - 开发者的网上家园