Skip to content
beginnerPhase 32 · JavaScript Fundamentals

Operators

Master arithmetic, comparison, logical, bitwise, and nullish operators.

30m
0 problems
Topic Progress0%

Arithmetic Operators

Arithmetic Operators

Basic Operators

let a = 10 + 5;    // 15 (addition)
let b = 10 - 5;    // 5 (subtraction)
let c = 10 * 5;    // 50 (multiplication)
let d = 10 / 5;    // 2 (division)
let e = 10 % 3;    // 1 (modulo/remainder)
let f = 2 ** 3;    // 8 (exponentiation)

Unary Operators

let x = 5;
++x;    // x is now 6 (prefix increment)
x++;    // x is now 7 (postfix increment)
--x;    // x is now 6 (prefix decrement)
x--;    // x is now 5 (postfix decrement)

Prefix vs Postfix

let a = 5;
let b = a++; // b = 5, a = 6 (postfix: use then increment)
let c = ++a; // c = 7, a = 7 (prefix: increment then use)

Compound Assignment

let x = 10;
x += 5;   // x = x + 5 = 15
x -= 3;   // x = x - 3 = 12
x *= 2;   // x = x * 2 = 24
x /= 4;   // x = x / 4 = 6
x %= 4;   // x = x % 4 = 2
x **= 3;  // x = x ** 3 = 8

Float Precision

0.1 + 0.2;    // 0.30000000000000004
0.1 + 0.2 === 0.3; // false

// Fix: use toFixed or compare with epsilon
Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON; // true

Comparison Operators

Comparison Operators

Equality

// Loose equality (==) - with coercion
5 == '5';      // true
0 == false;    // true
null == undefined; // true

// Strict equality (===) - no coercion
5 === '5';     // false
0 === false;   // false
null === undefined; // false

Inequality

// Loose inequality (!=)
5 != '5';      // false

// Strict inequality (!==)
5 !== '5';     // true

Relational

let a = 5, b = 10;

a > b;    // false (greater than)
a < b;    // true (less than)
a >= 5;   // true (greater than or equal)
a <= 10;  // true (less than or equal)

Logical Operators

// AND (&&) - returns first falsy or last value
true && true;     // true
true && false;    // false
'hello' && 'world'; // "world"
'' && 'world';    // ""

// OR (||) - returns first truthy or last value
true || false;    // true
false || true;    // true
'' || 'default';  // "default"
'hello' || 'world'; // "hello"

// NOT (!) - negates
!true;     // false
!false;    // true
!'';       // true
!'hello';  // false

Nullish Coalescing (??)

// Returns right side only for null/undefined
null ?? 'default';    // "default"
undefined ?? 'default'; // "default"
0 ?? 'default';       // 0
'' ?? 'default';      // ""

Optional Chaining (?.)

const user = { name: 'John' };

user?.address?.city;  // undefined (no error)
user?.getName?.();    // undefined (no error)

Logical Operators

Logical Operators

Short-Circuit Evaluation

// && stops at first falsy
false && console.log('never runs');

// || stops at first truthy
true || console.log('never runs');

Default Values

// Using ||
let name = inputName || 'Anonymous';

// Using ?? (better - only null/undefined)
let name = inputName ?? 'Anonymous';

Guard Clauses

// Instead of
if (user) {
  if (user.admin) {
    doSomething();
  }
}

// Use optional chaining
user?.admin && doSomething();

Logical AND Assignment

// ES2021
x &&= y;  // x = x && y
x ||= y;  // x = x || y
x ??= y;  // x = x ?? y

// Example
let config = {};
config.timeout ??= 5000; // Set only if undefined/null

Truth Table

| A | B | A && B | A || B | !A |
|---|---|--------|--------|-----|
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |

Practical Examples

// Guard pattern
const name = user && user.profile && user.profile.name;

// Modern: optional chaining
const name = user?.profile?.name;

// Default value pattern
const timeout = config?.timeout ?? 5000;

Practice Problems

0/3solved
Build Operators Component

Create a reusable React component implementing Operators. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Operators Testing

Write unit and integration tests for Operators using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Operators Performance

Optimize Operators for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What is the result of 10 % 3?

Question 1 options

2. What is the difference between == and ===?

Question 2 options

3. What does the && operator return?

Question 3 options

4. What is the difference between || and ???

Question 4 options

5. What does ++x do (prefix)?

Question 5 options

Flashcards

Question

What is the difference between && and ||?

Answer

&& returns first falsy or last value. || returns first truthy or last value.

Question

What is the nullish coalescing operator?

Answer

?? returns the right side only for null or undefined, not for 0 or empty string.

Question

What does optional chaining (?.) do?

Answer

Returns undefined if the value is null or undefined, instead of throwing an error.

Question

What is short-circuit evaluation?

Answer

&& and || stop evaluating as soon as the result is determined.

Question

What is the result of 2 ** 3?

Answer

8 - the exponentiation operator raises 2 to the power of 3.

Revision Notes

Key Takeaways

  • 1.Use === for strict equality comparison
  • 2.&& and || use short-circuit evaluation
  • 3.?? only treats null/undefined as nullish
  • 4.Optional chaining prevents null reference errors
  • 5.Compound assignment operators simplify code

Interview Tips

  • Know the difference between == and ===
  • Understand short-circuit evaluation
  • Explain the difference between || and ??
  • Be able to predict logical operator results

Cheat Sheet

Operators Cheat Sheet

Arithmetic

+  -  *  /  %  **
++x  x++  --x  x--
+=  -=  *=  /=  %=  **=

Comparison

==  ===  !=  !==
>  <  >=  <=

Logical

&&  ||  !
??  (nullish coalescing)
?.  (optional chaining)

Truth Table

| A | B | A && B | A || B |
|---|---|--------|--------|
| T | T | T | T |
| T | F | F | T |
| F | T | F | T |
| F | F | F | F |

Best Practices

  • Use === instead of ==
  • Use ?? for default values
  • Use ?. for null checks