JavaScript Tutorial

Operators

Arithmetic, comparison (== vs ===), logical, nullish coalescing (??)

Arithmetic Operators

JavaScript supports the standard mathematical operations.
OperatorActionExampleResult
+Addition / Concat10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 42.5
%Remainder10 % 31
**Exponentiation2 ** 38
javascript
// Addition or Concatenation?
console.log(1 + 2);     // 3
console.log("1" + 2);   // "12" (Wait! JS converted 2 to a string!)

Comparison Operators: == vs ===

This is one of the most important concepts in JavaScript.
  • == (Loose Equality): Checks value only. It tries to convert types to match (type coercion).
  • === (Strict Equality): Checks both value and type.
javascript
console.log(5 == "5");  // true (Value is 5)
console.log(5 === "5"); // false (Number vs String)
Best Practice: Always use === and !==. Loose equality leads to unpredictable bugs.

Logical Operators

OperatorNameLogic
&&ANDBoth must be true
||ORAt least one must be true
!NOTInvert the boolean
javascript
const isAdmin = true;
const hasSecretKey = false;

if (isAdmin && hasSecretKey) {
  // Only runs if BOTH are true
}

Nullish Coalescing (??)

Introduced in ES2020, this operator returns the right-hand value only if the left-hand value is null or undefined. This is safer than using ||, which also triggers for 0 or "".
javascript
const userScore = 0;

// Old Way (Potential Bug!)
const finalScore_bad = userScore || 50; 
// 0 is falsy, so it returns 50! (But 0 is a valid score!)

// New Way (Safe!)
const finalScore_good = userScore ?? 50; 
// Returns 0 because it's not null/undefined.

The Ternary Operator (?:)

A concise one-line if/else statement.
javascript
const age = 20;
const status = age >= 18 ? "Adult" : "Minor";

Summary

OperatorUsagePurpose
===a === bStrict equality (Recommended!)
==a == bLoose equality (Avoid!)
&& / ||a && bLogical operations
??a ?? bHandle null/undefined safely
!!statusBoolean inversion
typeoftypeof xGet data type
deletedelete obj.xRemove object property