Week4: Vid4.2 Comparison Operators and Type Coercion

by Lucille Kenney

HTML

<p>The equality operator converts the operands if they are not of the same type, then applies strict comparison. </p>
<p>
    If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the string operand is converted to a number if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.</p>
<p>From MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators</a></p>

JavaScript

var a = 6, b = 10;
if (a < b) {
    alert("1: you'll see this if a is less than b!");
} 

a = 6, b = "10";
if (a < b) {
    alert("2: you'll see this if a is less than b!");
}  

a = 6, b = "ten";
if (a < b) {
    alert("3: you'll see this if a is less than b");
}