Javascript比较运算符!= vs!== [英] Javascript Comparison Operators != vs !==

查看:146
本文介绍了Javascript比较运算符!= vs!==的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Javascript === vs ==:哪个“相等”我使用的运营商?

==和==之间的差异=在JavaScript中

我有两个要比较的变量。

I have two variables to compare.

结果不应该相等,我需要使用哪种情况!=和!==?

Result should not be equal, in which condition i need to use != and !== ?

因为我同时使用两者操作员它正常工作,但我需要确切地知道有什么区别。

because when i use both operator it is working properly, but i need to know exactly what is the difference.

推荐答案

关于差异的人类可读文字



使用!== === 将比 == / != 。前者将检查被比较的对象是否属于同一类型,以及值是否匹配。

Human readable text about their differences

Using !== and === will do a more strict compare than ==/!=. The former will check if the objects being compared are of the same type, as well as if the values matches.

使用 == 可以进行隐式演员表,参见下面的例子。

Using == will make it possible for an implicit cast to be made, see the below examples.

(0 ==  '0') // true
(0 === '0') // false

('' ==  0 ) // true, the string will implicitly be converted to an integer
('' === 0 ) // false, no implicit cast is being made






标准说什么?




What does the standard say?


11.9.6严格平等比较



算法比较x === y,其中x和y是值,产生true或false。这样的比较
执行如下:

11.9.6 The Strict 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. 如果Type(x)与Type(y)不同,则返回false。

  2. 如果Type(x)为Undefined,则返回true。

  3. 如果Type(x)为Null,则返回true。

  4. 如果Type(x)是Number,那么

  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

a。如果x是NaN,则返回false。

a. If x is NaN, return false.

b。如果y为NaN,则返回false。

b.If y is NaN, return false.

c。如果x与y的数字值相同,则返回true。

c. If x is the same Number value as y, return true.

d。如果x为+0且y为0,则返回true。

d. If x is +0 and y is 0, return true.

e。如果x是0且y是+0,则返回true。

e. If x is 0 and y is +0, return true.

f。返回false。

f. Return false.

如果Type(x)是String,则如果x和y完全相同的字符序列(相同的长度和相同的字符),则返回true
对应的头寸);否则,返回false。

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.









11.9.3抽象等式比较算法



比较x = = y,其中x和y是值,产生true或
false。这样的比较如下进行:

11.9.3 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. 如果Type(x)与Type(y)相同,那么

  1. If Type(x) is the same as Type(y), then

a。如果Type(x)是Undefined,则返回t rue。

a. If Type(x) is Undefined, return t rue.

b。如果Type(x)为Null,则返回true。

b. If Type(x) is Null, return true.

c。如果Type(x)是Number,那么

c. 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.

d。如果Type(x)是String,则如果x和y恰好是
,则返回true相同的字符序列(
对应位置中的长度和字符相同)。否则,返回false。

d. 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.

e。如果Type(x)是布尔值,如果x和y都为true或
都为false,则返回true。否则,返回false。
f。如果x和y引用同一个对象,则返回true。否则,返回false。

e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false. f. Return true if x and y refer to the same object. Otherwise, return false.


这篇关于Javascript比较运算符!= vs!==的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆