课程: JavaScript: Best Practices for Data

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Use triple-character equality operators

Use triple-character equality operators

- I have an array of some falsy values in JavaScript and I've written code to log the results of comparing each one to the string zero and to the value undefined. My code uses the double equal sign to check for equality which is one of two ways to check for equality in JavaScript Running my code, I get statements logged for the comparisons that are equivalent. And it turns out that the parser sees the string zero as equivalent to both the value zero, and false. And it sees the values null and undefined as equivalent. What? JavaScript has two different ways to compare equality, the algorithms in the parser for the double equal equality sign result in some unexpected equivalences. For instance, false is equivalent to zero, zero as a string, an empty string, and an empty array, but not an empty object, as well as an array containing only the value zero. Now typecasting my values can be helpful here. And double equal results…

内容