MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Comparison Operations

The abstract equality and inequality operators (== and !=) convert their operands if the operand types do not match. This type coercion is a common source of confusion about the results of these operators, in particular, these operators aren't always transitive as one would expect.


"" == 0; // true 
0 == "0"; // true 
"" == "0"; // false 
false == 0; // true
false == "0"; // true
"" != 0; // false 
0 != "0"; // false 
"" != "0"; // true 
false != 0; // false
false != "0"; // false

The results start to make sense if you consider how JavaScript converts empty strings to numbers.


Number(""); // 0
Number("0"); // 0
Number(false); // 0

The Solution

In the statement false B, both the operands are strings ("" and "0"), hence there will be no type conversion and since "" and "0" are not the same value, "" == "0" is false as expected.

One way to eliminate unexpected behavior here is making sure that you always compare operands of the same type. For example, if you want the results of numerical comparison use explicit conversion:


var test = (a,b) => Number(a) == Number(b);
test("", 0); // true;
test("0", 0); // true
test("", "0"); // true;
test("abc", "abc"); // false as operands are not numbers

Output

Or, if you want string comparison:


var test = (a,b) => String(a) == String(b);
test("", 0); // false;
test("0", 0); // true
test("", "0"); // false;

Output

Side-note: Number("0") and new Number("0") isn't the same thing! While the former performs a type conversion, the latter will create a new object. Objects are compared by reference and not by value which explains the results below.


Number("0") == Number("0"); // true;
new Number("0") == new Number("0"); // false

Output

Finally, you have the option to use strict equality and inequality operators which will not perform any implicit type conversions.


"" === 0; // false
0 === "0"; // false
"" === "0"; // false

Output


Conclusion

In this page (written and validated by ) you learned about Javascript Comparison Operations . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript boolean operators.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.