Operators and equality
An operator is a symbol that takes values and makes a new value. Follow a seven-line script as it stores a number and a string, works out 7 + 3 * 2 (multiplication first), joins text with +, and then compares 7 with "7" twice: == converts the string to a number and says true, while === never converts and says false.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01What is the value of 2 + 3 * 4?
* goes before +, so 3 * 4 = 12 is worked out first, then 2 + 12 = 14. Write (2 + 3) * 4 if you want 20.
02What does "5" + 2 give?
When either side of + is a string, + joins text. "5" + 2 becomes "5" + "2", which is the string "52".
03What is 7 == "7", and why?
Loose equality == converts values of different types before comparing. The string "7" becomes the number 7, and 7 equals 7.
04What is 7 === "7"?
Strict equality === never converts. A number and a string are different types, so they are not strictly equal.
05In let x = 10;, what does the = do?
A single = is the assignment operator: it stores the value on its right in the variable on its left. == and === are the ones that compare.
Glossary
- Operator
- A symbol such as +, *, === or && that takes one or more values and makes a new value.
- Operand
- A value an operator works on. In 7 + 6, the operands are 7 and 6.
- Assignment (=)
- Stores the value on the right in the variable on the left. It does not compare.
- Precedence
- The order operators are worked out in when there are several, for example * before +. Brackets change it.
- Concatenation
- Joining strings end to end. In JavaScript, + concatenates when either side is a string.
- Loose equality (==)
- Compares two values after converting them to the same type when their types differ.
- Strict equality (===)
- Compares two values without converting them: values of different types are never equal. !== is its opposite.
- Logical AND (&&)
- Gives true only when both sides are true (strictly, it returns one of its operands).
Notes and sources
- 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.
- If either side of + is a string, + joins text; otherwise it adds numbers. The other arithmetic operators, - * / and % (remainder), always work on numbers, so "7" - 3 is 4.
- && really returns one of its operands: the left one if it is falsy, otherwise the right one. With true and false on both sides, that is the same as "true only when both are true".
- One surprise even for ===: NaN === NaN is false. Use Number.isNaN() to check for NaN.
- The figure draws comparing as its own station so you can see it. In reality the JavaScript engine does every operator, including the comparisons.
- The script is real. Paste it into the browser console, or save it as ops.js and run node ops.js: it prints true, false, true.
- MDN: Expressions and operators
- MDN: Assignment (=)
- MDN: Operator precedence
- MDN: Addition (+)
- MDN: Equality (==)
- MDN: Strict equality (===)
- MDN: Equality comparisons and sameness
- MDN: Logical AND (&&)
- ECMAScript specification: IsLooselyEqual
- ECMAScript specification: IsStrictlyEqual
- ECMAScript specification: ApplyStringOrNumericBinaryOperator
Quiz Unlocks after the last step.