简体   繁体   中英

Does strict equality guarantees loose equality

I'm starting to write unit tests for my nodeJS/Javascript app with Mocha.

For my equality assertions tests I decided to use

  • assert.strictEqual to test for equality
  • assert.notEqual to test for difference

The idea behind this is to enforce the use of the strict equality operator ( === and !== ) but to be sure no error will be produced if == and != are used.

But it led me to a question :

For every possible a does a === b means a == b and does a !== b means a != b ?

The === and !== ensure that no only are the items considered 'equal' but that they are the same type - no type coercion is done:

"1" == 1 //true
"1" === 1 //false

It's the same for the inequality operator:

"2" != 1 //true
"2" !== 1 //true

"1" != 1 //false
"1" !== 1 //true

So, short answer, "For every possible a does a == b means a === b and does a != b means a !== b ?" - No.

否。例如0 !== false0 == false

The === comparison is stronger than == . A == comparison just checks if two values are equal, performing type conversions as needed. A === comparison check that both the type and the value are the same. So a === b implies a == b , and a != b implies a !== b .

Example:

"100" ==  100    ->  true
"100" === 100    ->  false
"100" !=  100    ->  false
"100" !== 100    ->  true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM