zoukankan      html  css  js  c++  java
  • js ==、!=、===、!== 的注意点

    1.  bool 和 数值型 再进行 ==  比较。会先将 bool 转成数值,true 转成 1, false 转成 0。

    console.log( false == 0 );    // 输出 true
    console.log( false == 1 );    // 输出 false
    console.log( false == 2 );    // 输出 false
    console.log( false == -1 );   // 输出 false
    console.log( true == 0 );     // 输出 false
    console.log( true == 1 );     // 输出 true
    console.log( true == 2 );     // 输出 false
    console.log( true == -1 );    // 输出 false

    2.  对象,数组等非简单类型的比较二者的引用是否相等。

    var a = new Array(1,2,3);
    var b = a;
    var c = new Array(3,2,1);
    var d = new Array(1,2,3);
    console.log( a == b );  // true
    console.log( a == c );  // false
    console.log( a == d );  // true

    3.  null 与 undefined 相等

    var a = null;
    var b = undefined;
    console.log( a == b );           // true
    console.log( a == undefined );   // true
    console.log( b == null );        // true

    4.  当数值、布尔值、字符串与对象类型比较时,先将对象类型转化成简单类型(先进行valueof(),再进行 tostring(), Date类型先进行tostring(),在进行valueof() )。

    5.  等同运算(===),如果二个值的类型不同,这它们不等同,null 与 undefined 不等同。 

    var a = null;
    var b = undefined;
    console.log( a === b );           // false
    console.log( a === undefined );   // false
    console.log( b === null );        // false

    6. != 和 == 运算规则相同,结果相反; !== 和 === 运算规则相同,结果相反

  • 相关阅读:
    八、JVM视角浅理解并发和锁
    七、JVM类加载机制
    六、JVM命令和工具
    五、jvm垃圾回收3(几种垃圾收集器)
    四、JVM垃圾回收2(垃圾收集算法)
    jvm引用类型
    三、JVM垃圾回收1(如何寻找垃圾?)
    【原创】Android 对话框的使用
    【原创】CMD常用命令:解决实际问题
    【原创】开机出现grub rescue,修复办法
  • 原文地址:https://www.cnblogs.com/lkcc/p/7518322.html
Copyright © 2011-2022 走看看