Commparsion in JavaScript

9412 ワード

Source:https://github.com/getify/You-Dont-Know-JS/blob/master/up%20&%20going/ch2.md#equality
False Values in JS
  • "" (empty string)
  • 0、  -0、  NaN (invalid  number)
  • null、  undefined
  • Simple Rules Using====
  • If eif ther value(aka side)in a could be the  true or  false value、avoid  == and use  ===.
  • If either value in a copariso could be of these specific values(0""、or  [] -- empty array),avoid  == and use  ===.
  • In all. other cases,you're safe to use  ==.Not only is it safe,but in many cases it simplifies your code in a way that improves readability.
  • Object(including function,array)Comprsion
    Because those values are actually held by reference,both  == and  === comprisos will simply check whether the references match,not anything about the undering values.array s are by default coerced to  string s by simply juning all the values with comas(,)in between.
    var a = [1,2,3];
    
    var b = [1,2,3];
    
    var c = "1,2,3";
    
    
    
    a == c;     // true
    
    b == c;     // true
    
    a == b;     // false
    Inequality
  • string values can also be compred for inequality、using typical alphabeetic rules("bar" < "foo")
  • var a = 41;
    
    var b = "42";
    
    var c = "43";
    
    
    
    a < b;      // true
    
    b < c;      // true
  •  If both values in the  < comppariso are  string s、as it is with  b < c,the comprion is made lexicographcally.
  • But if one or both is not a  string、as it is with  a < b、then both values are coerced to be  number s,and a typical numeric compriso occurs.
  • var a = 42;
    
    var b = "foo";
    
    
    
    a < b;      // false
    
    a > b;      // false
    
    a == b;     // false
  • b value is being coerced to the「invalid number value」  NaN in the  < and  > comprisos.
  • NaN is neither greater-than nor less-than,equal-to any other value.Hence,it returns false.
  • Source:http://www.ecma-international.org/ecma-262/5.1/
    The comprison x == y,where x and y ar values、produces true or false.Such a comprison is performed as follows:
  • If  Type(x)is the same as  Type(y)、then
  • If  Type(x)is Udefined,return true.
  • If  Type(x)is Null,return true.
  • If  Type(x)is Number,then
  • If x is NaN,リセット false.
  • If y is NaN,リセット false.
  • If x is the same Number value as y,リセット true.
  • If x is +0 and y is −0,リセット true.
  • If x is −0 and y is +0,リセット true.
  • Return false.
  • If  Type(x)is String,then return true if x and y arexactly the same sequence of characters.Othe rwise,return false.
  • If  Type(x)is Boolean,return true if x and y arboth true or both false.Otherwise,return false.
  • Return true if x and y refer to the same object.Other wise,return false.
  • If x is null and y is undefined,return true.
  • If x is undefined and y is null,リセット true.
  • If  Type(x)is Number and  Type(y)is String,return the relt of the comprison x ==  ToNumber(y).
  • If  Type(x)is String and  Type(y)is Number,return the relt of the comprison  ToNumber(x)== y.
  • If  Type(x)is Boolean,return the relt of the comprison  ToNumber(x)== y.
  • If  Type(y)is Boolean,return the relt of the comprison x ==  ToNumber(y).
  • If  Type(x)is eigther String or Number and  Type(y)is Object,return the reult of the comprison x ==  ToPrimitive(y).
  • If  Type(x)is Object and  Type(y)is eigther String or Number,return the rereult of the comprison  ToPrimitive(x)== y.
  • Return false.
  • The comprison x === y,where x and y ar values、produces true or false.Such a comprison is performed as follows:
  • If  Type(x)is different from  Type(y)、リセット false.
  • If  Type(x)is Udefined,return true.
  • If  Type(x)is Null,return true.
  • If  Type(x)is Number,then
  • If x is NaN,リセット false.
  • If y is NaN,リセット false.
  • If x is the same Number value as y,リセット true.
  • If x is +0 and y is −0,リセット true.
  • If x is −0 and y is +0,リセット true.
  • Return false.
  • If  Type(x)is String,then return true if x and y arexactly the same sequence of characters;otherwise,return false.
  • If  Type(x)is Boolean,return true if x and y arboth true or both false;otherwise,return false.
  • Return true if x and y refer to the same object.Other wise,return false.