Comparsion in JavaScript

來源:互聯網
上載者: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 be the true or false value, avoid == and use ===.
  • If either value in a comparison 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) Comparsion

Because those values are actually held by reference, both == and ===comparisons will simply check whether the references match, not anything about the underlying values.

arrays are by default coerced to strings by simply joining all the values with commas (,) in between.

var a = [1,2,3];var b = [1,2,3];var c = "1,2,3";a == c;     // trueb == c;     // truea == b;     // false
Inequality
  • string values can also be compared for inequality, using typical alphabetic rules ("bar" < "foo")
var a = 41;var b = "42";var c = "43";a < b;      // trueb < c;      // true
  •  If both values in the < comparison are strings, as it is with b < c, the comparison is made lexicographically.
  • But if one or both is not a string, as it is with a < b, then both values are coerced to be numbers, and a typical numeric comparison occurs.
var a = 42;var b = "foo";a < b;      // falsea > b;      // falsea == b;     // false
  • b value is being coerced to the "invalid number value" NaN in the < and > comparisons.
  • 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 comparison x == y, where x and y are 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 String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
      5. If Type(x) is Boolean, return true if x and y are both true or both false. 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 String,
      return the result of the comparison x == ToNumber(y).
    5. If Type(x) is String and Type(y) is Number,
      return the result of the comparison ToNumber(x) == y.
    6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
    7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
    8. If Type(x) is either String or Number and Type(y) is Object,
      return the result of the comparison x == ToPrimitive(y).
    9. If Type(x) is 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 x and y are 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, 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 String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
  6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  7. Return true if x and y refer to the same object. Otherwise, return false.

Comparsion in JavaScript

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.