When writing a program, we don't just store values—we also need to perform operations on those values.
For example, we may want to add two numbers, compare two values, check whether something is true, or assign a new value to a variable.
JavaScript provides operators for performing these tasks.
What is an Operator?
An operator is a symbol or keyword that tells JavaScript to perform a specific operation on one or more values.
For example:
let result = 10 + 5;Here:
10and5are operands.+is the operator.resultstores the final value.
The output is:
15Types of JavaScript Operators
JavaScript has several types of operators:
Operator Type | Purpose |
|---|---|
Arithmetic | Perform mathematical calculations |
Assignment | Assign values to variables |
Comparison | Compare values |
Logical | Combine conditions |
Increment & Decrement | Increase or decrease a value |
Ternary | Write simple conditional expressions |
String | Work with strings |
Bitwise | Perform operations on binary values |
Nullish Coalescing | Handle |
Let's understand the most important ones.
1. Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
Operator | Meaning | Example |
|---|---|---|
| Addition |
|
| Subtraction |
|
| Multiplication |
|
| Division |
|
| Remainder |
|
| Exponentiation |
|
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:
13
7
30
3.3333333333333335
1
1000The % operator gives the remainder after division.
For example:
console.log(10 % 3);Output:
1because 10 divided by 3 leaves a remainder of 1.
2. Assignment Operators
Assignment operators are used to assign values to variables.
The basic assignment operator is:
=Example:
let age = 20;Here, 20 is assigned to age.
JavaScript also provides shorthand assignment operators.
Operator | Example | Equivalent To |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For example:
let score = 50;
score += 10;
console.log(score);Output:
60score += 10 is simply a shorter way of writing:
score = score + 10;3. Comparison Operators
Comparison operators are used to compare two values.
They return either:
trueor:
falseOperator | Meaning |
|---|---|
| Equal value |
| Equal value and type |
| Not equal value |
| Not equal value or type |
| Greater than |
| Less than |
| Greater than or equal to |
| Less than or equal to |
For example:
let age = 20;
console.log(age > 18);
console.log(age < 18);
console.log(age === 20);Output:
true
false
true== vs ===
This is particularly important in JavaScript.
console.log(5 == "5");Output:
trueBut:
console.log(5 === "5");Output:
false== allows type coercion, while === checks both value and type.
As a general rule, prefer === and !== in modern JavaScript unless you have a specific reason to use loose equality.
4. Logical Operators
Logical operators are used to combine or reverse conditions.
There are three main logical operators:
Operator | Name | Meaning |
|---|---|---|
| AND | Both conditions must be true |
` | ` | |
| NOT | Reverses a Boolean value |
AND (&&)
let age = 25;
let hasLicense = true;
console.log(age >= 18 && hasLicense);Output:
trueBoth conditions are true.
OR (||)
let isWeekend = false;
let isHoliday = true;
console.log(isWeekend || isHoliday);Output:
trueAt least one condition is true.
NOT (!)
let isLoggedIn = true;
console.log(!isLoggedIn);Output:
falseThe ! operator reverses the Boolean value.
These operators become especially useful when we learn conditional statements.
5. Increment and Decrement Operators
Sometimes we simply want to increase or decrease a number by 1.
For this, JavaScript provides:
++ → Increase by 1
-- → Decrease by 1Example:
let count = 5;
count++;
console.log(count);Output:
6Similarly:
let count = 5;
count--;
console.log(count);Output:
4You'll commonly see these operators when working with loops.
6. Ternary Operator
The ternary operator is a short way of writing a simple condition.
Its syntax is:
condition ? valueIfTrue : valueIfFalse;For example:
let age = 20;
let result = age >= 18 ? "Adult" : "Minor";
console.log(result);Output:
AdultIt's basically a shorter version of an if...else statement.
We'll learn this properly when we cover conditional statements.
7. String Operators
The + operator can also be used to join strings.
let firstName = "John";
let lastName = "Smith";
let fullName = firstName + " " + lastName;
console.log(fullName);Output:
John SmithThis is called string concatenation.
JavaScript also provides template literals, which are often cleaner for combining strings:
let name = "John";
let age = 21;
console.log(`My name is ${name} and I am ${age} years old.`);Output:
My name is John and I am 21 years old.8. Nullish Coalescing Operator
The ?? operator is used when we want to provide a default value if something is null or undefined.
let username = null;
let name = username ?? "Guest";
console.log(name);Output:
GuestHere, because username is null, JavaScript uses "Guest" instead.
This operator is especially useful when handling optional data.
Operator Precedence
Just like mathematics, JavaScript follows certain rules when multiple operators are used in an expression.
For example:
let result = 10 + 5 * 2;
console.log(result);The result is:
20not 30, because multiplication happens before addition.
You can use parentheses when you want to make the order clear:
let result = (10 + 5) * 2;
console.log(result);Output:
30Using parentheses is often a good idea when an expression could become confusing.
A Practical Example
Let's combine several operators:
let price = 100;
let quantity = 3;
let total = price * quantity;
let discount = total >= 300 ? 20 : 0;
let finalPrice = total - discount;
console.log(finalPrice);Output:
280Here we're using:
*for multiplication>=for comparison? :for the ternary operator-for subtraction=for assignment
This is how operators come together in real programs.
Conclusion
Operators are the tools JavaScript gives us to perform calculations, compare values, assign data, and make decisions.
The most important ones to remember initially are:
Arithmetic → + - * / %
Assignment → = += -= *=
Comparison → === !== > < >= <=
Logical → && || !
Increment → ++
Decrement → --
Ternary → ? :Once you understand operators, you're ready to start using them to make decisions in your programs with conditional statements.