Operators in JavaScript
Operators allow you to perform actions on values. You can use them for calculations, comparisons, assigning values, and even logical decisions. Operators are an essential part of every programming language, and learning them properly will help you write stronger conditions, loops, and functions.
As a college student or fresher, mastering operators early will make your logic building much easier.
JavaScript has several types of operators. In this chapter, we will learn the most commonly used ones:
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
String Operators
Unary Operators
Let’s understand each type step-by-step.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (remainder) |
| ** | Exponent (power) |
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 operators assign values to variables.
| Operator | Meaning |
|---|---|
| = | Assign value |
| += | Add and assign |
| -= | Subtract and assign |
| *= | Multiply and assign |
| /= | Divide and assign |
Example:
let x = 10;
x += 5;
x -= 3;
x *= 2;
x /= 4;
console.log(x);
Output:
6
Explanation:
10 + 5 = 15
15 - 3 = 12
12 × 2 = 24
24 ÷ 4 = 6
3. Comparison Operators
These operators compare two values and return true or false.
| Operator | Meaning |
|---|---|
| == | Equal (with coercion) |
| === | Strict equal |
| != | 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 > 7);
console.log(3 <= 3);
Output:
truefalsetruetrueImportant:
==compares values but not types===compares both value and type
Always prefer === for accurate comparison.
4. Logical Operators
Logical operators facilitate decision-making within conditional statements and loops.
| Operator | Meaning |
|---|---|
| && | AND |
| || | OR |
| ! | NOT |
AND (&&)
Returns true only if both conditions are true.
console.log(5 > 2 && 10 > 5);
Output:
trueOR (||)
Returns true if any one condition is true.
console.log(5 > 10 || 10 > 5);
Output:
trueNOT (!)
Flips the value.
console.log(!true);
console.log(!false);
Output:
falsetrue5. String Operator (+)
The + operator joins two strings.
let first = "Hello";
let second = "World";
console.log(first + " " + second);
Output:
Hello World
If even one value is a string, JavaScript converts the other into a string automatically.
Example:
console.log("Age: " + 20);
Output:
Age: 206. Unary Operators
Unary operators work on a single value.
Increment (++)
let a = 5;
a++;
console.log(a);
Output:
6
Decrement (--)
let b = 10;
b--;
console.log(b);
Output:
9
Example Program Using Multiple Operators
let math = 40;
let science = 45;
let total = math + science;
let average = total / 2;
console.log("Total:", total);
console.log("Average:", average);
let passed = average >= 35;
console.log("Passed:", passed);
Output:
Total: 85Average: 42.5Passed: trueThis example illustrates how arithmetic, comparison, and logical operators interact.
Why Operators Are Important
You will use operators in:
Calculations
Conditions (if-else)
Loops
Functions
Form validation
API responses
Logic building
Operators make your program smart and interactive.
Practice Tasks (Do It Yourself)
Calculate the total and average of three marks.
Check if a number is even or odd using
%.Compare two ages and print who is older.
Use
&&and||to check if a student passed.Try
"10" + 5and"10" - 5and observe the difference.