你真的了解null吗?

今天在知乎上看到这样一个问题,关于javascript的,null > 0  返回的是false,null >= 0 返回的是true, null == 0 返回的是false。这是为什么呢?

其中有高人回答并且找到了权威的资料,简单的说就是,在关系运算符中,null 和0进行比较时,null首先会转化成number类型,Number(null)返回的值是0,这下就可以很明白的理解,null > 0为什么返回false。而null >=0就返回true。然而有同学该疑问了,为什么在相等运算符中,==  null 的值为什么没有转化呢,这个就要问问,javascript语言之父了,null在和0相比较的时候,就是没有转化成number类型。这个要切记。写到这里,要感谢@Franky这位大牛的博客。我也是看了他的博客才有感而发。

说到这,就不得不说说,关系运算符的相等运算符== 和===之间的区别。我认为这个比较重要,有的同学搞不明白,有必要说一下。

== 和===运算符用于比较两个值是否相等,当然对他们的定义不尽相同。两个运算符允许任意类型的操作数,如果操作数相等,则返回true,否则返回false。===也称为,严格相等运算符(strict equality),有时候也称为恒相等运算符(identity operator)。它用来检测两个操作数是否相等,这里的相等的定义非常宽松,可以允许进行类型转换。

Javascript中支持,= == === 运算符,应当理解,三者分别的含义,赋值,相等,严格相等。并在编码的过程中严格使用。

!= 和!== 运算符的检测是== ===运算符的取反,如果两个值通过==比较时true,则!= 返回的值为false,那么如果两个值通过=== 比较得到的值是true,则!== 返回的值是false。

下面说说严格相等运算符=== 首先计算其操作数的值,然后比较这两个值,比较过程中没有任何类型的转换。

  1. l  如果两个类型不同,则它们不相等。
  2. l  如果两个值都是null 或者undefined 则它们不相等。
  3. l  如果两个值都是true或者false,则它们相等。
  4. l  如果其中一个值是NaN或者两个值都是NaN,则它们不相等。NaN和其他任何值都是不相等的,包括本身。通过x!== x 来判断x是否为NaN 因为你只有在x为NaN的时候,这个表达式的值才为true。
  5. l  如果两个值为数字且数值相等,则它们相等,如果一个值为0,另一个值为-0,则它们同样相等。
  6. l  如果两个值为字符串,且对应位上的16位数完全相等,则它们相等。如果它们的长度或内容不同,则它们不等。两个字符串可能含义完全一样且所显示出的字符也一样,但是具有不同编码的16位值。Javascript不对unicode进行标准化的转换,因此像这样的字符串通过=== 和== 运算符的比较结果也不相等。
  7. l  如果两个引用值指向同一个对象,数组,或函数,则它们是相等的。如果指向不同的对象,则它们是不相等的,尽管两个对象具有完全一样的属性。

l  相等运算符== 和恒相等运算符相似,但相等运算符的比较并不严格,如果两个操作数不是同一类型,那么相等运算符会尝试进行一些类型转换,然后比较。

  1. l  如果两个操作数的类型相同,泽合上文所述的严格相等比较规则一致,如果严格相等比较结果相等,那么比较结果相等,如果严格相等比较结果不相等,那么比较结果不相等。
  2. 如果两个操作数类型不相同。==运算符也可能认为他们相等,检测相等将会遵守如下规则和类型转换。
  3. l  如果一个值是null另一个为undefined,则他们相等。
  4. l  如果一个值是数字,另一个是字符串,先将字符串转换成数字,然后使用转换后的值比较。
  5. l  如果一个值是true,则将其转换成1再比较,如果一个值为false,则将其转换成0在比较。
  6. l  如果一个值为对象,另一个值为数字或者字符串,则将对象转化成原始值,然后进行比较。对象通过valueof() 方法或者toString()转换为原始值,.Javascript语言核心的内置类首先尝试使用valueOf在尝试使用toString,除了日期类,日期类只能使用Tostring转换。那些不是javascript语言核心中的对象则通过各自的实现中定义的方法转换为原始值。
  7. l  其他不同类型之间的比较均不相等。


附:大牛博客地址:http://www.cnblogs.com/_franky/archive/2012/09/26/2703723.html

ES3 的 ">" 运算符: 

  The Greater-than Operator ( > )
The production RelationalExpression : RelationalExpression > ShiftExpression is evaluated as follows:
1. Evaluate RelationalExpression.
2. Call GetValue(Result(1)).
3. Evaluate ShiftExpression.
4. Call GetValue(Result(3)).
5. Perform the comparison Result(4) < Result(2). 
6. If Result(5) is undefined, return false. Otherwise, return Result(5).

 

  ES3 的">=" 运算符:

  The Greater-than-or-equal Operator ( >= )

The production RelationalExpression : RelationalExpression >= ShiftExpression is evaluated as follows:
1. Evaluate RelationalExpression.
2. Call GetValue(Result(1)).
3. Evaluate ShiftExpression.
4. Call GetValue(Result(3)).
5. Perform the comparison Result(2) < Result(4). (see 11.8.5).
6. If Result(5) is true or undefined, return false. Otherwise, return true.

 

  ES3 的 "==" 运算符 :

  The Equals Operator ( == )

The production EqualityExpression : EqualityExpression == RelationalExpression is evaluated as
follows:
1. Evaluate EqualityExpression.
2. Call GetValue(Result(1)).
3. Evaluate RelationalExpression.
4. Call GetValue(Result(3)).
5. Perform the comparison Result(4) == Result(2). (see 11.9.3).
6. Return Result(5).

    

      ES3 关于 内部关系运算的算法实现 :

  The Abstract Relational Comparison Algorithm

The comparison x < y, where x and y are values, produces true, false, or undefined (which indicates that
at least one operand is NaN). Such a comparison is performed as follows:
1. Call ToPrimitive(x, hint Number).
2. Call ToPrimitive(y, hint Number).
3. If Type(Result(1)) is String and Type(Result(2)) is String, go to step 16. (Note that this step differs
from step 7 in the algorithm for the addition operator + in using and instead of or.)
4. Call ToNumber(Result(1)).
5. Call ToNumber(Result(2)).
6. If Result(4) is NaN, return undefined.
7. If Result(5) is NaN, return undefined.
8. If Result(4) and Result(5) are the same number value, return false.
9. If Result(4) is +0 and Result(5) is −0, return false.
10. If Result(4) is −0 and Result(5) is +0, return false.
11. If Result(4) is +∞, return false.
12. If Result(5) is +∞, return true.
13. If Result(5) is −∞, return false.

14. If Result(4) is −∞, return true.
15. If the mathematical value of Result(4) is less than the mathematical value of Result(5)—note that
these mathematical values are both finite and not both zero—return true. Otherwise, return false.
16.If Result(2) is a prefix of Result(1), return false. (A string value p is a prefix of string value q if q
can be the result of concatenating p and some other string r. Note that any string is a prefix of itself,
because r may be the empty string.)
17. If Result(1) is a prefix of Result(2), return true.
18.Let k be the smallest nonnegative integer such that the character at position k within Result(1) is
different from the character at position k within Result(2). (There must be such a k, for neither string
is a prefix of the other.)
19. Let m be the integer that is the code point value for the character at position k within Result(1).
20. Let n be the integer that is the code point value for the character at position k within Result(2).
21. If m < n, return true. Otherwise, return false.

           

 

  ES3 关于 内部相等性运算的算法实现 :

The Abstract Equality Comparison Algorithm

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), go to step 14.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.
4. If Type(x) is not Number, go to step 11.
5. If x is NaN, return false.
6. If y is NaN, return false.
7. If x is the same number value as y, return true.
8. If x is +0 and y is −0, return true.
9. If x is −0 and y is +0, return true.
10. Return false.
11.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.
12. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
13.Return true if x and y refer to the same object or if they refer to objects joined to each other (see
13.1.2). Otherwise, return false.
14. If x is null and y is undefined, return true.
15. If x is undefined and y is null, return true.

16.If Type(x) is Number and Type(y) is String,return the result of the comparison x == ToNumber(y).
17.If Type(x) is String and Type(y) is Number,return the result of the comparison ToNumber(x) == y.
18. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
19. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
20.If Type(x) is either String or Number and Type(y) is Object,return the result of the comparison x == ToPrimitive(y).
21.If Type(x) is Object and Type(y) is either String or Number,return the result of the comparison ToPrimitive(x) == y.
22. Return false.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值