相等比较运算符与严格比较运算符-用JavaScript

This article discusses the difference between strict equality operator and equality operator in JavaScript.

本文讨论JavaScript中严格相等运算符和相等运算符之间的区别。

The Need:

需要:

Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when writing code.

由于JavaScript在执行比较时执行隐式类型转换,因此在编写代码时必须考虑到这一点。

The equality operator is denoted as “==” while strict equality operator is denoted as “===”.

等式运算符表示为“ ==”,而严格等式运算符表示为“ ===”。

Equality operator: (==)

等于运算符:(==)

This comparison operator returns true only when they are equal without comparing their types.

仅当它们相等而不比较它们的类型时,此比较运算符才返回true。

If two operands are of different types, JavaScript converts the operands and then applies strict comparison between them.

如果两个操作数的类型不同,则JavaScript会转换操作数,然后在它们之间进行严格的比较。

For example,

例如,

true == 1;     //true, because 'true' is converted to 1 and then compared.

true == 1; // true,因为'true'转换为1,然后进行比较。

"2" == 2;       //true, because 2 (the integer) is converted to "2" (the string) and then compared.

“ 2” == 2; // true,因为2(整数)将转换为“ 2”(字符串),然后进行比较。

Strict equality operator: (===)

严格等于运算符:(===)

This === operator is exists in JavaScript because JavaScript is weakly typed language. For other languages, it is not needed for type comparison because other languages already have reserved words available to perform those functions. But for JavaScript, these special comparison operators make more sense.

JavaScript中存在此===运算符,因为JavaScript是弱类型语言。 对于其他语言,类型比较不需要它,因为其他语言已经具有可用于执行这些功能的保留字。 但是对于JavaScript,这些特殊的比较运算符更有意义。

You can use this operator only when exact type of operand is important for comparison.

仅当操作数的确切类型对于比较重要时,才可以使用此运算符。

For example,

例如,

true === 1;   //false

true === 1; //假

"2" === 2;     // false

“ 2” === 2; //错误

What JavaScript does for comparison?

什么JavaScript做比较?

When type conversion is needed, JavaScript converts String, Number, Boolean or Object operands as follows:

当需要类型转换时,JavaScript会按以下方式转换字符串,数字,布尔值或对象操作数:

2. If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.

2.如果其中一个操作数是布尔值,则布尔值操作数为true时将转换为1,否则为+0。

3. If an object is compared with a number or string, JavaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a String or Number value, using the valueOf and toString methods of the objects. If this attempt to convert the object fails, a run-time error is generated.

3.如果将对象与数字或字符串进行比较,JavaScript会尝试返回该对象的默认值。 操作员尝试使用对象的valueOf和toString方法将对象转换为原始值(字符串或数字值)。 如果此转换对象的尝试失败,则会生成运行时错误。

4. If both operands are objects, they're compared as objects and the equality test is true only if both refer the same object.[1]

4.如果两个操作数都是对象,则将它们作为对象进行比较,并且仅当两个对象都引用同一个对象时,相等测试才为true。 [1]

Performance:

性能:

If we consider theoretically, because strict equality operator (===) does not do conversion for comparison is faster than == operator.

如果从理论上考虑,因为严格相等运算符(===)不会进行比较转换,所以比==运算符要快。

Now let us execute below programs and we will calculate the time for their execution:

现在让我们执行以下程序,我们将计算它们的执行时间:

var n = 0;
while(true) {
    n++;
    if(n==100000) break;
}

and

var n = 0;
while(true) {
    n++;
    if(n===100000) break;
}
Here are results: (averaged): 结果如下:(平均):

For the first one (i.e. for == operator): 24 ms

对于第一个(即==运算符): 24 ms

For the second one (i.e. for === operator): 26 ms

对于第二个(即对于===运算符): 26 ms

If you see the total time required to execute for both comparisons, for over 100000 iterations, the difference is very less.

如果您看到执行两次比较所需的总时间,对于100000多次迭代,则差异很小。

So strict equality operator (===) should be used for type safety and code equality reason, 'performance' should not the reason to use.

因此,出于类型安全和代码相等的原因,应使用严格的相等运算符(===),而不应使用“性能”的原因。

Finally here are some examples:

最后是一些示例:

        alert('' == '0');  // false
        alert(0 == '');  // true
        alert(0 == '0');  // true
        alert(false == 'false');  // false
        alert(false == '0');  // true
        alert(false == undefined);  // false
        alert(false == null);  // false
        alert(null == undefined);  // true
        alert(' \t\r\n ' == 0);  // true

For strict comparison (using === ) for above all examples, results into false.

对于以上所有示例,为了严格比较(使用===),结果均为false。

Conclusion:

结论:

It is recommended to use strict equality operator(===), when one has to perform comparison with type check.

当必须与类型检查进行比较时,建议使用严格相等运算符(===)。

Good references:

好的参考文献:

1. https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators

1. https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators

2. http://www.ecma-international.org/publications/standards/Ecma-262.htm

2. http://www.ecma-international.org/publications/standards/Ecma-262.htm

3. http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use#957602

3. http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use#957602

翻译自: https://www.experts-exchange.com/articles/9629/Equal-comparison-operator-vs-Strict-comparison-operator-in-JavaScript.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值