JavaScript: Type Coercion & the Equal/Non equal operators

Type Coercion can affect comparison of values with Equal & Non equal operators so its recommended to uses '===' and '!==' for strict comparison

by sjmcpherson

HTML

<div id="elm"></div>

JavaScript

var elm = document.getElementById("elm");


//The '==' or '!=' operator uses Type Coercion values of different types can be compared
if(0 == false)
     elm.innerText += "0 == false -> true";

//The '===' or '!==' operator is used for strict comparison where values must be of the same type.
if(0 === false)
     elm.innerText += "0 === false -> false";

//Values can be converted to real booleans by adding the !! infront of a value
elm.innerHTML += "<br/>"
elm.innerText += !!0
elm.innerHTML += "<br/>"
elm.innerText += !!{}