Home  >  Article  >  Web Front-end  >  Javascript study notes: equality symbol and strict equality symbol

Javascript study notes: equality symbol and strict equality symbol

PHPz
PHPzOriginal
2016-05-16 16:30:401727browse

This chapter introduces the equality symbols and strict equality symbols in JavaScript in detail. Friends in need can refer to it.

Javascript has two methods to determine whether two values ​​are equal.

Equality symbol

The equality symbol consists of two equal signs: ==
Javascript is a weakly typed language. This means that the equality symbol will cast the type in order to compare two values.

""           ==   "0"           // false
0            ==   ""            // true
0            ==   "0"           // true
false        ==   "false"       // false
false        ==   "0"           // true
false        ==   undefined     // false
false        ==   null          // false
null         ==   undefined     // true
" \t\r\n"    ==   0             // true

The above code shows the result of type conversion, so we know that using the equality symbol == is a bad programming habit. Due to the complex type conversion mechanism in Javascript, the resulting errors will become difficult to track down.

In addition, type coercion will also have a certain impact on performance. For example, when a string is compared with a number, it will be coerced into a number.

Strict equality symbol

The strict equality symbol consists of three equal signs: ===
It operates similarly to the equality symbol, but the strict equality symbol does not Will perform forced type conversion operation.

""           ===   "0"           // false
0            ===   ""            // false
0            ===   "0"           // false
false        ===   "false"       // false
false        ===   "0"           // false
false        ===   undefined     // false
false        ===   null          // false
null         ===   undefined     // false
" \t\r\n"    ===   0             // false

The above code makes the code clearer. If the two values ​​are of different types, false is returned directly, which will also improve performance.

Comparing Objects

Although == and === are called equality symbols, when one of the two values ​​compared is an object, the behavior will Big difference.

{} === {};                   // false
new String('foo') === 'foo'; // false
new Number(10) === 10;       // false
var foo = {};
foo === foo;                 // true

Here, it is no longer just comparing whether the two values ​​are equal, it will determine whether the two values ​​refer to the same object instance. This behavior is more like a pointer in C.

Summary

It is strongly recommended to only use the strict equality symbol ===. If we need to do type conversion, we can do explicit type conversion before comparison instead of relying on Javascript's own complex cast method.

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn