Operators in JavaScript
Operators are symbols that perform actions on values.
Every line of JavaScript you write will likely use some operator—whether you’re calculating values, comparing numbers, checking conditions, or updating variables.
Understanding operators is important for college students and freshers because they form the foundation of programming logic.
In this chapter, you will learn:
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Unary Operators
Ternary Operator
Let’s learn each of them with simple explanations and examples.
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Remainder (modulus) | a % b |
| ** | Power / Exponent | a ** b |
Example:
let a = 10;
let b = 3;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
console.log(a ** b);
Output:
137303.3333333333333335110002. Assignment Operators
These assign values to variables.
| Operator | Meaning | Example |
|---|---|---|
| = | Assign | a = 5 |
| += | Add and assign | a += 3 (a = a + 3) |
| -= | Subtract and assign | a -= 2 |
| *= | Multiply and assign | a *= 2 |
| /= | Divide and assign | a /= 2 |
Example:
let x = 10;
x += 5;
console.log(x);
x -= 3;
console.log(x);
x *= 2;
console.log(x);
Output:
15
12
24
3. Comparison Operators
Used to compare two values.
They return true or false.
| Operator | Meaning |
|---|---|
| == | Equal (value only) |
| === | Strict equal (value + type) |
| != | Not equal |
| !== | Strict not equal |
| > | Greater than |
| < | Less than |
| >= | Greater or equal |
| <= | Less or equal |
Example:
console.log(5 == "5");
console.log(5 === "5");
console.log(10 > 5);
console.log(10 <= 10);
Output:
truefalsetruetrueImportant:
Use === instead of == to avoid unexpected results.
4. Logical Operators
Used in conditions.
| Operator | Meaning |
|---|---|
| && | AND |
| || | OR |
| ! | NOT |
Example:
let age = 20;
console.log(age > 18 && age < 30);
console.log(age < 18 || age > 30);
console.log(!true);
Output:
truefalsefalse5. Unary Operators
Unary operators work with a single value.
Increment (++) and Decrement (--)
let n = 5;
n++;
console.log(n);
n--;
console.log(n);
Output:
6
5
typeof operator
Used to check the type of data.
console.log(typeof 10);
console.log(typeof "Hello");
console.log(typeof true);
console.log(typeof []);
Output:
numberstringbooleanobjectArrays show as “object” (JavaScript design behavior).
6. Ternary Operator (Short if-else)
The ternary operator provides a compact way to write conditions.
Syntax:
condition ? valueIfTrue : valueIfFalse
Example:
let age = 18;
let result = age >= 18 ? "Eligible" : "Not Eligible";
console.log(result);
Output:
EligibleReal-Life Example: Pass/Fail Check
let marks = 40;
let status = marks >= 35 ? "Pass" : "Fail";
console.log(status);
Output:
PassReal-Life Example: Discount Calculation
let price = 500;
let discount = price > 300 ? 50 : 20;
console.log("Discount:", discount);
Output:
Discount: 50Example Program (All Operators Combined)
let a = 10;
let b = 5;
console.log(a + b);
console.log(a > b);
console.log(a === 10);
let msg = (a % 2 === 0) ? "Even" : "Odd";
console.log(msg);
a += 20;
console.log(a);
Output:
15truetrue
Even
30Common Mistakes Beginners Make
Using == instead of ===
Using assignment (=) instead of comparison (== or ===)
Forgetting that ! changes true ? false
Confusing && with ||
Misusing increment and decrement operators