if("0" == true) if("0") if("1" === true) if(!!"0" == true) if("0" != true) if("0" !== true) var someVariable; if(someVariable) if(someVariable == true) if(!someVariable === true) if(!!someVariable === true)
if(“0” == true)
"0" == true
resolves to false. Why? Because both sides of the equal are converted to numerics and then evaluated. This gives us, 0 == 1 which is in fact false.
is true. This is because there is no conversion needed. Any string is truthy.
OK. So then, what would you expect
if(“1” === true)
to be? If you said, “true” you would be wrong. And just when you thought you were getting the hang of this.
Do you think you are getting the hang of this yet? OK. What does the next statement evaluate to?
if(!!”0” == true)
This evaluates to true. If you thought it would be false, you probably applied the !! after the types were converted. But the operator precedence on this is to apply the ! operators first. So ! some valid string is false and ! false is true. So true == true is true.
The ! operator
The next two examples I gave were to simply point out that != is the opposite of double equal and !== is the opposite of triple equal. Beyond that, they work the same.
So, if(“0” != true) would evaluate to true and if(“0” !== true) would also evaluate to true.
The last set of examples illustrates what happens if a variable is undefined or null.
Since we never gave someVariable a value, it is undefined and so if(someVariable) would evaluate to false. Undefined and null variables evaluate to falsy.
However, that does not mean that it is a boolean value.
So, if(someVariable == true) evaluates to false, if(someVariable === false) evaluates to false, if(!someVariable === true) evaluates to true and if(!!someVariable === true) evaluates to false.
The main implication of all of this is that if you are going to compare two variables for equality, you should first convert them to a common type and then compare them using the triple equals operator.
However, if you are just evaluating a variable for truthiness, using just the variable, as in if(someVariable) is clear enough. You don’t gain much by using if(!!someVariable) or even if(!!someVariable === true) syntax.
- WebForms vs MVC–The War Is Over - September 25th, 2014
- Create A Desktop Application using Angular, Bootstrap and C# - October 15th, 2015
- Are You Doing Angular Right? - November 5th, 2015
- Adventures Working With Angular’s $scope - November 26th, 2015
- Using Gulp to Bundle, Minify, and Cache-bust - January 28th, 2016
- Reactions to React JS and Associated Bits - March 17th, 2016
- An Explanation of the Flux Pattern - March 31st, 2016
- Ext JS 6 by Sencha - The Good, The Bad, The Ugly - April 7th, 2016
- Do This To Increase Your Client Side Web Development Speed - April 21st, 2016
- ES2015 Code Coverage and Jest (React JS Unit Testing) - May 5th, 2016
- 4 Reasons To Drop MVVM - July 27th, 2016
- You Can Start Using Node Today - August 2nd, 2016
- TypeScript and Electron The Right Way - September 6th, 2016