MOCKSTACKS
EN

# 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``````

## 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.