Comparsion in JavaScript

Source: Internet
Author: User

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 either value (aka side) in a comparison could true false is the or value, avoid and use == === .
    • If either value in a comparison could is of these specific values ( 0 , "" , or [] --empty array), avoid == and Use === .
    • In All other cases, you ' re safe to use == . Not only was it safe, but in many cases it simplifies your code in a by that improves readability.

Object (including function, array) comparsion

Because Those values is actually held by reference, both == and === comparisons would simply check whether the refer Ences match, not anything about the underlying values.

arrayS is by default coerced to string s by simply joining all the values with commas ( , ) in between.

var a = [n/a]; var b = [n/a]; var c = "All-in-all" = = C;     // trueb = = C;     // trueA = = B;     // false
Inequality
    • stringValues can also is compared for inequality, using typical alphabetic rules ( "bar" < "foo" )
var a = all; var b = "a"; var c = "$"< b;      // trueb < C;      // true
    • If both values in the < comparison string is S, as it's with b < c , the comparison is made lexicographically.
    • But if one or both was not a string , as it was with a < b , then both values was coerced to be number s, and a typical numeri C comparison occurs.
var a =; var b = "foo"< b;      // falsea > b;      // falseA = = B;     // false
    • bValue is being coerced to the ' Invalid number value ' in the and NaN < > comparisons.
    • NaNis neither Greater-than nor Less-than, equal-to any and value. Hence, it returns false.

Source:http://www.ecma-international.org/ecma-262/5.1/

The comparison x = = y , where and is x y values, produces true or false. Such A comparison is performed as follows:

    1. If Type (x) is the same as Type (y), then
      1. If Type (x) is Undefined, return true.
      2. If Type (x) is Null, return true.
      3. If type ( x ) is number, then
        1. if  x  is  nan , return  false .
        2. if  y  is  NaN , return  false .
        3. if  x  is the same number value as  y , return  true .
        4. if  x  is  +0  and  y  is  −0 , return  true .
        5. if  x  is  −0  and  y  is  +0 , return  true .
        6. return  false .
      4. If Type (x) is a String, then return true if x and y are exactly the same sequence of Cha Racters (same length and same characters in corresponding positions). Otherwise, return false.
      5. If Type (x) is a Boolean, return true if x and y are both true or both fals E. Otherwise, return false.
      6. Return True if x and y refer to the same object. Otherwise, return false.
    2. If x is null and y is undefined, return true.
    3. If x is undefined and y is null, return true.
    4. If Type (x) is number and Type (y) is a String,
      Return the result of the comparison x = = Tonumber (y).
    5. If Type (x) is a String and Type (y) is number,
      Return the result of the comparison tonumber (x) = = y.
    6. If Type (x) is a Boolean, return the result of the comparison tonumber (x) = = y.
    7. If Type (y) is a Boolean, return the result of the comparison x = = Tonumber (y).
    8. If Type (x) is either a String or number and Type (y) is Object,
      Return the result of the comparison x = = toprimitive (y).
    9. If Type (x) is an Object and Type (y) is either String or number,
      Return the result of the comparison toprimitive (x) = = y.
    10. Return false.

The comparison x = = = y , where and x y is values, produces true or false. Such A comparison is performed as follows:

  1. If type (x) is different from Type (y), return false.
  2. If Type (x) is Undefined, return true.
  3. If Type (x) is Null, return true.
  4. If Type (x) is number and then
    1. If x is NaN, return false.
    2. If y is NaN, return false.
    3. If x is the same number value as y, return true.
    4. If x is +0 and y is −0, return true.
    5. If x is −0 and y is +0, return true.
    6. Return false.
  5. If Type (x) is a String, then return true if x and y are exactly the same sequence of Cha Racters (same length and same characters in corresponding positions); Otherwise, return false.
  6. If Type (x) is a Boolean, return true if x and y are both true or both fals e; Otherwise, return false.
  7. Return True if x and y refer to the same object. Otherwise, return false.

Comparsion in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.