Operators in JavaScript
Master JavaScript operators including arithmetic, comparison, logical, assignment, and the language-specific quirks of type coercion
Operators are the building blocks of expressions in JavaScript. They let you do arithmetic, compare values, combine boolean conditions, and assemble strings. As a dynamically and weakly typed language, JavaScript brings some unique behavior to operators — most notably type coercion, where the language quietly converts values between types when you mix them in an expression.
JavaScript’s multi-paradigm nature means operators show up in surprising places: arrow functions use =>, the spread operator (...) unpacks iterables, and short-circuit evaluation (&&, ||, ??) is routinely used as control flow. In this tutorial we’ll work through the everyday operators, then look at the famous quirks that make JavaScript both powerful and occasionally bewildering.
By the end you’ll know how to perform calculations, compare values safely with ===, build strings with template literals, and recognize the coercion gotchas that trip up newcomers.
Arithmetic, Comparison, and Logical Operators
Create a file named operators.js:
| |
Notice that / always produces a floating-point result — there is no separate integer division operator. To truncate toward zero, use Math.trunc. Logical operators short-circuit: && stops at the first falsy value, || stops at the first truthy value, and both return the value that stopped them, not necessarily a boolean.
Strict vs Loose Equality and Type Coercion
JavaScript’s weak typing makes equality the most error-prone operator in the language. The rule of thumb: always prefer === over ==.
Create a file named equality.js:
| |
The + operator is the famous trouble-maker: with any string operand it becomes string concatenation, while every other arithmetic operator forces numeric coercion. The nullish coalescing operator ?? (ES2020) was added specifically to avoid the || trap where valid falsy values like 0 or "" get replaced by defaults.
Assignment, String, and Modern Operators
Create a file named modern_ops.js:
| |
Template literals (backtick strings) are the modern replacement for + concatenation — they’re more readable and support multi-line strings without escape characters. Optional chaining (?.) and nullish coalescing (??) work together as a safer way to navigate possibly-missing object properties.
Running with Docker
| |
Expected Output
Output from operators.js:
a + b = 22
a - b = 12
a * b = 85
a / b = 3.4
a % b = 2
a ** b = 1419857
Math.trunc(a / b) = 3
-n = -10
++n = 11
n-- = 11
n = 10
a > b: true
a === 17: true
a !== b: true
isAdult && hasTicket: false
isAdult || hasTicket: true
!isAdult: false
Output from equality.js:
"5" == 5: true
0 == false: true
null == undefined: true
"" == 0: true
"5" === 5: false
0 === false: false
null === undefined: false
"3" + 4: 34
"3" - 4: -1
"3" * "4": 12
count || 10: 10
count ?? 10: 0
Output from modern_ops.js:
after +=: 15
after -=: 12
after *=: 24
after /=: 6
after **=: 36
cached: default
In 1843, Ada wrote the first algorithm.
In 1843, Ada wrote the first algorithm.
status: minor
combined: [ 1, 2, 3, 4, 5 ]
user?.profile?.name: Grace
user?.address?.city: undefined
2 + 3 * 4 ** 2 = 50
Key Concepts
/always returns a float — JavaScript has only one number type (64-bit float); useMath.truncorMath.floorfor integer division===is almost always what you want — strict equality compares without type coercion; reserve==only when you specifically need thenull == undefinedshortcut+is overloaded — with any string operand it concatenates, otherwise it adds; other arithmetic operators force numeric coercion- Logical operators return values, not booleans —
a || breturnsaif truthy, otherwiseb; this enables short-circuit defaulting ??distinguishes “missing” from “falsy” — unlike||, the nullish coalescing operator only falls through onnull/undefined, preserving0,"", andfalse- Template literals replace string concatenation — backticks with
${expr}interpolation are clearer than chained+operators - Optional chaining (
?.) safely accesses properties on possibly-null objects, returningundefinedinstead of throwing - Precedence matters:
**>*/%>+-> comparisons >&&>||??— when in doubt, parenthesize
Comments
Loading comments...
Leave a Comment