From the course: JavaScript: Best Practices for Data

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use triple-character equality operators

Use triple-character equality operators - JavaScript Tutorial

From the course: JavaScript: Best Practices for Data

Start my 1-month free trial

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…

Contents