Operators and equality
An operator takes one or two values and produces one. The arithmetic operators are the ones every language has, and the plus sign is the one that is special: with two numbers it adds, with a string on either side it joins. That single exception explains most of the surprises in beginner code, and the fix is usually to convert the value before the operator sees it.
The comparison operators are more interesting. JavaScript has two of them, === and ==, and the difference is not style: the loose one converts its operands and the strict one does not. This lesson shows what each one answers for the same six pairs, so that the choice becomes a rule rather than a preference.
Arithmetic and precedence
The program below walks through the arithmetic operators once. There is one number type, so division produces a fraction rather than a truncated integer, and the remainder operator keeps the sign of the left operand. Exponentiation is **, and multiplication binds tighter than addition, as it does in mathematics.
The string lines show the two ways the plus sign behaves. With a string on the left it joins, and the number becomes text; with a minus it converts the string to a number and subtracts. The last two lines are the operators that choose between values instead of computing one.
console.log('7 + 2 =', 7 + 2);
console.log('7 / 2 =', 7 / 2);
console.log('7 % 2 =', 7 % 2);
console.log('2 ** 10 =', 2 ** 10);
console.log('1 + 2 * 3 =', 1 + 2 * 3);
console.log('(1 + 2) * 3 =', (1 + 2) * 3);
console.log('"3" + 4 =', '3' + 4);
console.log('"3" - 4 =', '3' - 4);
const empty = 0;
const filled = 1;
console.log('short circuit:', empty || 'fallback', filled && 'second');
const missing = null;
const zero = 0;
console.log('nullish:', missing ?? 'default', zero ?? 'default');
node operators.js
7 + 2 = 9
7 / 2 = 3.5
7 % 2 = 1
2 ** 10 = 1024
1 + 2 * 3 = 7
(1 + 2) * 3 = 9
"3" + 4 = 34
"3" - 4 = -1
short circuit: fallback second
nullish: default 0
"3" + 4 is the text "34" and "3" - 4 is the number -1, and neither result is what a reader of the source might expect. When a value arrives from a form or a file it is text, and Number(value) before the arithmetic is the habit that removes the whole class of bugs.
Comparison operators
Numbers compare by value and strings compare by their UTF-16 code units, character by character. That is why "2" < "10" is false: the first characters are compared, and the character 2 comes after the character 1. When the two operands have different types, the comparison converts them, which produces the surprises on the last two lines.
console.log('2 < 10:', 2 < 10);
console.log('"2" < "10":', '2' < '10');
console.log('"apple" < "banana":', 'apple' < 'banana');
console.log('null >= 0:', null >= 0);
const nothing = undefined;
console.log('undefined == 0:', nothing == 0);
node comparisons.js
2 < 10: true
"2" < "10": false
"apple" < "banana": true
null >= 0: true
undefined == 0: false
null >= 0 is true because null converts to 0 in a numeric comparison, while undefined == 0 is false because undefined converts to NaN and every comparison with NaN is false. Sorting text and comparing numbers are two different jobs, and the array lessons give each one its function.
Equality: === against ==
=== answers true only when the two values have the same type and the same value. == first converts one operand to the type of the other, following a table with a dozen cases, and then compares. The program below runs both operators over the same pairs, so the difference is visible line by line.
const pairs = [
[1, '1'],
[0, ''],
[0, false],
[null, undefined],
['1', true],
[[], false],
];
for (const [left, right] of pairs) {
const shownLeft = JSON.stringify(left);
const shownRight = JSON.stringify(right);
console.log(`${shownLeft} vs ${shownRight}: == ${left == right}, === ${left === right}`);
}
console.log('Number.isNaN(NaN):', Number.isNaN(NaN));
console.log('Object.is(NaN, NaN):', Object.is(NaN, NaN));
const zero = 0;
const negativeZero = -0;
console.log('Object.is(0, -0):', Object.is(zero, negativeZero));
console.log('0 === -0:', zero === negativeZero);
node equality.js
1 vs "1": == true, === false
0 vs "": == true, === false
0 vs false: == true, === false
null vs undefined: == true, === false
"1" vs true: == true, === false
[] vs false: == true, === false
Number.isNaN(NaN): true
Object.is(NaN, NaN): true
Object.is(0, -0): false
0 === -0: true
Every pair is loose-equal and none is strictly equal, which is the reason the course writes === everywhere. Object.is is the third comparison: it behaves like === except for two values, and it is the only one that treats two NaN values as equal and two zeros of different signs as different.
Logical and nullish operators
|| returns its left operand when that operand is truthy and its right operand otherwise, and && does the opposite. Both stop evaluating as soon as the answer is known, which is why a call on the right runs only when the left side allows it. The values that count as false are false, 0, "", null, undefined and NaN; every other value is true.
?? is the newer operator, and it answers with the right operand only when the left one is null or undefined. The difference from || matters for a number that may legitimately be zero: the last line of the program keeps the 0, while || would replace it with the default.
Exercises
- Add a line with "3" * "4" to operators.js. What type is the result, and which operator converted the strings?
- Add [] == false and [] === false to equality.js. Which one is true, and what does that say about the loose table?
- Replace the ?? in the last line of operators.js with ||. Which number changes, and why?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which operator compares two values without converting them?
Answer
===
Strict equality requires the same type and the same value, and it is the operator this course uses.
-
What does
"3" + 4evaluate to?Answer
"34"
A string on either side makes the plus sign join, so the number becomes text and the result is a string.
-
What does
"3" - 4evaluate to?Answer
-1
The minus sign has no string form, so it converts the string to a number and subtracts.
-
What does this program print?
console.log(1 == "1", 1 === "1");Answer
true false
The loose operator converts the string and the strict one finds two different types.
-
What does
null == undefinedevaluate to?Answer
true
Loose equality is the one comparison that treats null and undefined as equal, and === does not.
-
What does
Object.is(NaN, NaN)return?Answer
true
Object.is treats two NaN values as equal, while === does not.
-
What does
"2" < "10"evaluate to?Answer
false
Two strings compare character by character, and the character 2 comes after the character 1.
-
Which operator returns the right operand only for null and undefined?
Answer
??
The nullish operator keeps 0 and the empty string, which || would replace with the default.
-
What does this program print?
const zero = 0; console.log(zero || "default", zero ?? "default");Answer
default 0
Zero is falsy, so || takes the default, while ?? keeps it because it is neither null nor undefined.
-
What is the result of
2 ** 10?Answer
1024
The double asterisk is exponentiation, so the result is 2 raised to the tenth power.
The code on this page was run with Node.js 24.22.1 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev