DEV Community

Karthick Narayanan
Karthick Narayanan

Posted on

JavaScript Part 3: Operators, Arithmetic, Assignment & Comparisons

An operator is a symbol that performs an operation on one or more values. Think of operators as the verbs of JavaScript — they make things happen.


1. JS Arithmetic Operators

Arithmetic operators do math.

Operator Name Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2
% Modulus (remainder) 10 % 3 1
** Exponentiation 2 ** 4 16
++ Increment x++ adds 1
-- Decrement x-- subtracts 1

Basic examples

let a = 20;
let b = 6;

console.log(a + b);  // 26
console.log(a - b);  // 14
console.log(a * b);  // 120
console.log(a / b);  // 3.3333...
console.log(a % b);  // 2  (remainder when 20 is divided by 6)
console.log(a ** 2); // 400 (20 squared)
Enter fullscreen mode Exit fullscreen mode

Modulus %

The % operator gives you the remainder after division. It's surprisingly useful!

console.log(10 % 3); // 1  → because 10 = (3 × 3) + 1
console.log(15 % 5); // 0  → because 15 divides evenly by 5
Enter fullscreen mode Exit fullscreen mode

💡 A common use: checking if a number is even or odd.

let num = 8;
if (num % 2 === 0) {
  console.log("Even"); // Even
}

Increment ++ and Decrement --

These are shortcuts to add or subtract 1 from a variable.

let count = 5;

count++; // same as count = count + 1
console.log(count); // 6

count--; // same as count = count - 1
console.log(count); // 5
Enter fullscreen mode Exit fullscreen mode

2. JS Assignment Operators

Assignment operators are used to set or update the value of a variable. You already know the basic one — =.

Operator Example Same as
= x = 10 x = 10
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5
**= x **= 2 x = x ** 2

Examples

let score = 100;

score += 50;  // score is now 150
score -= 20;  // score is now 130
score *= 2;   // score is now 260
score /= 4;   // score is now 65
score **= 2;  // score is now 4225

console.log(score); // 4225
Enter fullscreen mode Exit fullscreen mode

These shorthand operators save you from writing the variable name twice. Instead of:

score = score + 50; // repetitive
Enter fullscreen mode Exit fullscreen mode

You simply write:

score += 50; // clean and short ✅
Enter fullscreen mode Exit fullscreen mode

3. JS Comparison Operators

Comparison operators compare two values and always return a boolean — either true or false.

Operator Meaning Example Result
== Equal to (loose) 5 == "5" true
=== Equal to (strict) 5 === "5" false
!= Not equal (loose) 5 != 3 true
!== Not equal (strict) 5 !== "5" true
> Greater than 10 > 5 true
< Less than 10 < 5 false
>= Greater than or equal 5 >= 5 true
<= Less than or equal 4 <= 5 true

Basic examples

let age = 18;

console.log(age > 16);  // true
console.log(age < 16);  // false
console.log(age >= 18); // true
console.log(age <= 17); // false
console.log(age == 18); // true
console.log(age != 20); // true
Enter fullscreen mode Exit fullscreen mode

== vs === — the most important difference

This is one of the most common beginner mistakes in JavaScript.

== (double equals) compares only the value, automatically converting types if needed.

=== (triple equals) compares both the value AND the type — no conversion.

console.log(5 == "5");  // true  ← JS converts "5" to a number first
console.log(5 === "5"); // false ← number vs string — not the same type!

console.log(0 == false);  // true  ← JS converts false to 0
console.log(0 === false); // false ← number vs boolean — not the same type!
Enter fullscreen mode Exit fullscreen mode

Using comparisons in conditions

let marks = 75;

if (marks >= 50) {
  console.log("Pass ✅");
} else {
  console.log("Fail ❌");
}
// Output: Pass ✅
Enter fullscreen mode Exit fullscreen mode

Quick Recap

Arithmetic operators — do math (+, -, *, /, %, **, ++, --)

Assignment operators — set or update variable values (=, +=, -=, *=, /=)

Comparison operators — compare values and return true or false (==, ===, !=, !==, >, <, >=, <=)

Tip Remember
Use % to check even/odd or remainders
Use += instead of x = x + value
Always use === not == to avoid type bugs

Top comments (0)