JavaScript Operators and Control Flow Essentials

Oeprators

Arithmetic

Operator Meaning
+ addition
- subtraction
* multiplication
/ division
% remainder

Floating-point math is approximate:

console.log(0.1 + 0.2); // 0.30000000000000004

Never compare two floats directly; instead check if their difference is within an acceptable tolerance.

Increment / Decrement

Syntax Name Side-effect
++x pre-increment increment, then yield value
x++ post-increment yield value, then increment
--x pre-decrement decrement, then yield value
x-- post-decrement yield value, then decrement
let a = 5;
console.log(++a); // 6  (a is 6)
console.log(a++); // 6  (a becomes 7 afterwards)

Comparison

Operator Meaning Example
== loose equality 5 == '5' → true
=== strict equality 5 === '5' → false
!= loose inequality 5 != '5' → false
!== strict inequality 5 !== '5' → true
> < greater / less than
>= <= greater/less or equal

Logical

Operator Name Truth table (A op B)
&& AND true only if both true
` `
! NOT flips the boolean

Short-circuit evaluation:

const result = value || 'default'; // left side truthy? keep it, else right side
const flag  = isReady && doWork(); // proceed only if isReady is truthy

Assignment shortcuts

Shorthand Expanded form
x += y x = x + y
x -= y x = x - y
x *= y x = x * y
x /= y x = x / y
x %= y x = x % y

Precedence snapshot

Highest → lowest: () → unary (!, ++) → arithmetic → comparison → logical (&& before ||) → assignment.


Control Flow

if statement

if (age >= 18) {
  console.log('Access granted');
}

if…else

if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
  console.log('Leap year');
} else {
  console.log('Common year');
}

else if chain

if (score >= 90) {
  grade = 'A';
} else if (score >= 80) {
  grade = 'B';
} else if (score >= 70) {
  grade = 'C';
} else if (score >= 60) {
  grade = 'D';
} else {
  grade = 'E';
}

Ternary operator

const padded = num < 10 ? '0' + num : String(num);

switch

switch (fruit) {
  case 'apple':
    price = 3.5;
    break;
  case 'durian':
    price = 35;
    break;
  default:
    price = null;
}
  • switch uses strict (===) comparison.
  • Omitting break causes fall-through.
  • Prefer switch when testing many discrete values; use if for ranges or complex conditions.

Tags: javascript Operators control-flow if-else switch

Posted on Sun, 23 Aug 2026 16:27:19 +0000 by dodgei