博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Comparsion in JavaScript
阅读量:4984 次
发布时间:2019-06-12

本文共 3817 字,大约阅读时间需要 12 分钟。

Source:

False Values in JS

  • "" (empty string)
  • 0-0NaN (invalid number)
  • nullundefined

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:

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

    1. If (x) is the same as (y), then
      1. If (x) is Undefined, return true.
      2. If (x) is Null, return true.
      3. If (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 (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 (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 (x) is Number and (y) is String,
      return the result of the comparison x == (y).
    5. If (x) is String and (y) is Number,
      return the result of the comparison (x) == y.
    6. If (x) is Boolean, return the result of the comparison (x) == y.
    7. If (y) is Boolean, return the result of the comparison x == (y).
    8. If (x) is either String or Number and (y) is Object,
      return the result of the comparison x == (y).
    9. If (x) is Object and (y) is either String or Number,
      return the result of the comparison (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 (x) is different from (y), return false.
  2. If (x) is Undefined, return true.
  3. If (x) is Null, return true.
  4. If (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 (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 (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.

转载于:https://www.cnblogs.com/lilixu/p/4612316.html

你可能感兴趣的文章
BZOJ 4484: [Jsoi2015]最小表示(拓扑排序+bitset)
查看>>
FastDFS 自动部署和配置脚本
查看>>
有道面试
查看>>
跟牛牛老师学习python自动化的第六天
查看>>
bzoj 1067: [SCOI2007]降雨量
查看>>
利用Flume将本地文件数据中收集到HDFS
查看>>
html5的优缺点
查看>>
wget下载文件
查看>>
Swagger使用--在一个Controller中使用相同(类似)参数的方法
查看>>
Vim 加 Gmail 变身 Vmail
查看>>
P1294 高手去散步
查看>>
IOS用IB快速适配iPhone5
查看>>
一次谷歌面试趣事
查看>>
(Z)使用mp4v2将H264+AAC合成mp4文件
查看>>
HDU 1735 字数统计(模拟+一点点贪心的思想)
查看>>
因为smb和nfs挂掉导致客户端开机启动不了
查看>>
Python百题计划
查看>>
(5) Orchard 开发之 Localization and NullLocalizer
查看>>
分类算法(1)--KNN
查看>>
每日记载内容总结3
查看>>