javascript interactive

JavaScript Interactive — a console crash course


1.4 Type Coercion & Equality

Warning: Your browser may be outdated or unsupported.

The JavaScript console is known to display some of the examples incorrectly in Internet Explorer 8 or older.

There are four equality operators in JavaScript: ==, !=, === & !==. The longer operators compare values as-is, whereas the shorter ones apply type coercion.

>>>
"text" == 'text'
true
>>>
"text" != "another text"
true
>>>
1 == "1"
true
Type coercion in effect. The string value is converted to a number before the comparison.
>>>
1 === "1"
false
>>>
null == undefined
true
Unexpected but nonetheless convenient. Using type coercion, a test for null will also catch undefined values.
>>>
"" + 13
false
Type coercion can be forced explicitly. For example, a string result is forced by concatenating with an empty string. Note that the + operator has multiple meanings.
>>>
"" + true
"true"
>>>

Index 1 2 3 4 5 6 7