Operators in TypeScript
Learn arithmetic, comparison, logical, and assignment operators in TypeScript, plus modern operators like nullish coalescing and optional chaining
Operators are the symbols that let you combine values into expressions—adding numbers, comparing strings, or chaining conditions. TypeScript inherits its entire operator set from JavaScript, so the runtime behavior is identical. What TypeScript adds is type checking around operators: the compiler verifies that you are using operators on compatible types, catching mistakes like multiplying a string by an object before your code ever runs.
Because TypeScript is a multi-paradigm language with static, structural typing, operators serve a dual role. At runtime they do exactly what JavaScript does. At compile time, TypeScript uses operators to narrow types—for example, after a typeof x === "string" check, the compiler knows x is a string inside that branch. This makes operators part of TypeScript’s type-safety story, not just arithmetic.
In this tutorial you will work through arithmetic, comparison, logical, and assignment operators, then explore the modern operators that make TypeScript code concise and null-safe: nullish coalescing (??), optional chaining (?.), and logical assignment.
Arithmetic, Comparison, and Logical Operators
The everyday operators behave just as they do in most C-style languages. One thing to note: TypeScript has no separate integer type. All numbers are 64-bit floating point, so / always performs floating-point division.
Create a file named operators.ts:
| |
A few TypeScript-specific notes:
- Always prefer
===and!==(strict equality) over==and!=. The strict forms do not perform type coercion. In fact, TypeScript will raise a compile error if you use==to compare two values whose types cannot overlap. **is exponentiation, so2 ** 10is1024. The**=assignment form raises a variable to a power in place.+is overloaded: with numbers it adds, with strings it concatenates. TypeScript checks the operand types and infers the result type accordingly.
Modern Operators: Nullish Coalescing and Optional Chaining
TypeScript shines when dealing with values that might be null or undefined. These modern operators are where typed JavaScript becomes safer and more expressive than older patterns.
Create a file named modern_operators.ts:
| |
The key distinction:
??only treatsnullandundefinedas “missing.” The older||operator treats any falsy value—0,"",false,NaN—as missing. When0or an empty string is a valid value,??is the correct choice.?.short-circuits toundefinedthe moment any link in the chain isnullorundefined, avoiding “cannot read property of undefined” runtime errors.??=combines the two ideas: it assigns a default only when the existing value isnullorundefined.
Running with Docker
Use the official Node.js image and run the examples with ts-node, which compiles and executes TypeScript in one step.
| |
Expected Output
Running operators.ts produces:
Arithmetic Operators:
a + b = 22
a - b = 12
a * b = 85
a / b = 3.4
a % b = 2
a ** b = 1419857
Comparison Operators:
a > b: true
a < b: false
a >= b: true
a === 17: true
a !== b: true
Logical Operators:
AND: false
OR: true
NOT: true
Assignment Operators:
After += 5: 15
After -= 3: 12
After *= 2: 24
After **= 2: 576
String Concatenation:
Type + Script = TypeScript
Operator Precedence:
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
Ternary:
Status: adult
Running modern_operators.ts produces:
Nullish Coalescing:
Name: Anonymous
With ||: 10
With ??: 0
Optional Chaining:
Email: no email
Logical Assignment:
Theme: dark
Key Concepts
- No integer type: every number is a 64-bit float, so
/is always floating-point division (17 / 5is3.4, not3). - Use strict equality:
===and!==avoid type coercion, and TypeScript flags comparisons between incompatible types at compile time. **for exponentiation: both the binary**and the**=assignment form are available.+is overloaded: it adds numbers and concatenates strings, with TypeScript inferring the result type from the operands.??vs||: nullish coalescing only falls back onnull/undefined, while||falls back on any falsy value—choose??when0or""are valid.- Optional chaining
?.prevents runtime crashes by short-circuiting toundefinedon a missing link in a property chain. - Logical assignment (
??=,&&=,||=) combines a logical test with assignment to set defaults concisely. - Operators participate in type narrowing: checks like
typeof x === "string"tell the compiler the type ofxinside that branch, making operators part of TypeScript’s safety model.
Comments
Loading comments...
Leave a Comment