JS engineabsumjoined011let a = 7;2let b = "7";3let sum = a + 3 * 2;4let joined = b + 3;5console.log(a == b);6console.log(a === b);7console.log(sum > 10 && a !== 0);Consoletrueops.js:5falseops.js:6trueops.js:77713"7""73"truetruetruefalse
Back: the JavaScript engine, memory (a, b, sum, joined) and the compare stationFront: the script ops.js and the developer consoleTokens: numbers are round, strings are square, true/false are pillsAt the back, the JavaScript engine with four memory boxes, and a compare station with a lamp that lights for true. In front, the script ops.js and the developer console. Numbers travel as round discs, strings as square tiles. * goes before +, and + with a string joins text. == converts the string "7" to the number 7 before comparing (true); === never converts (false). The figure draws comparing as its own station so you can see it. In reality the JavaScript engine does every operator, including the comparisons.

What operators do

  1. An operator is a symbol that takes values, called operands, and makes a new value. In 7 + 6 the operator is + and the result is 13. This script uses arithmetic, comparison and logical operators.

Arithmetic

Comparing values

What happens

// step 1/9 · operators
operator    a symbol that makes a new value
operands    the values it works on
            7 + 6   →  13
arithmetic  +  -  *  /  %
comparison  ==  ===  !=  !==  <  >  <=  >=
logical     &&  ||  !
assignment  =

The full rules for == (IsLooselyEqual in the ECMAScript specification) have several cases. A string compared with a number is turned into a number first, and null == undefined is true. === (IsStrictlyEqual) never converts: values of different types are never equal. The figure draws comparing as its own station so you can see it. In reality the JavaScript engine does every operator, including the comparisons.