JavaScript is one of the most powerful and popular programming languages used to build interactive web applications. Whether you’re creating a simple website or a complex web app, JavaScript is almost always involved.
One important part of writing good JavaScript code is understanding operators. Operators help you perform actions like doing calculations, comparing two values, or making decisions in your programs. They are used all the time, even in the smallest pieces of code.
In this beginner-friendly guide, we’ll take a closer look at what operators
are, the different types of operators you’ll use, and how to work with them
using simple, real-world examples. By the end, you’ll feel more confident in
writing better JavaScript code.
What Are Operators in JavaScript?
Operators are like the secret tools in JavaScript that help you get things done. They are symbols or keywords that allow you to perform operations on values — which are often called operands.
Think of it this way: if values are ingredients, operators are the kitchen
tools that help you mix, slice, and cook them into something amazing!
For example, the + operator simply adds two numbers together: 5 + 3 gives you 8. Easy, right?
On the other hand, the === operator is like a super strict security guard — it
checks if two values are exactly the same, not just in value but also in
type. So 5 === '5' would actually be false because one is a number and the
other is a string!
Operators are everywhere in your code. You’ll use them for basic math, for comparing values, for making decisions, for assigning values to variables — and much more. They’re one of the essential building blocks that bring your JavaScript programs to life.
Once you get the hang of operators, you’ll find that writing logic and solving
problems becomes much smoother — and honestly, a lot more fun too!
How Many Types of Operators Are There in JavaScript?
JavaScript includes several types of operators:
- Arithmetic
Operators
- Assignment
Operators
- Comparison
Operators
- Logical
(Boolean) Operators
- Conditional
(Ternary) Operator
- Bitwise
Operators
- Type
Operators
- String
Operators
- Comma
Operator
Let’s dive into each type.
What are Arithmetic Operators in JavaScript?
Arithmetic operators are the basic tools you use in
JavaScript to do math.
Whenever you need to add prices together, calculate someone's age, find
averages, or even figure out how many items are left — arithmetic operators
come to the rescue!
They take numbers (called operands) and perform
calculations to give you a result.
Think of them as the calculators inside your JavaScript code!
Here’s a breakdown of the main arithmetic operators:
1. Addition (+)
Adds two numbers together.
let apples = 5;
let bananas = 3;
let totalFruits = apples + bananas;
console.log(totalFruits); // Output: 8
You now have 8 fruits in total!
2. Subtraction (-)
Subtracts the second number from the first.
let books = 10;
let borrowed = 4;
let remainingBooks = books - borrowed;
console.log(remainingBooks); // Output: 6
You still have 6 books left after lending some to
friends.
3. Multiplication (*)
Multiplies two numbers.
let pricePerItem = 50;
let quantity = 3;
let totalPrice = pricePerItem * quantity;
console.log(totalPrice); // Output: 150
You’re buying 3 items at 50 bucks each, so you pay 150 in
total!
4. Division (/)
Divides the first number by the second.
let totalMarks = 400;
let subjects = 4;
let average = totalMarks / subjects;
console.log(average); // Output: 100
Each subject gets an average of 100 marks.
5. Modulus (%)
Gives the remainder after division.
let candies = 17;
let kids = 5;
let candiesLeft = candies % kids;
console.log(candiesLeft); // Output: 2
After giving candies equally to all 5 kids, 2 candies are
left!
6. Exponentiation (**)
Raises a number to the power of another number.
let base = 2;
let exponent = 4;
let result = base ** exponent;
console.log(result); // Output: 16
2 raised to the power of 4 (2 × 2 × 2 × 2) is 16.
7. Increment (++)
Increases a number by 1.
let score = 10;
score++;
console.log(score); // Output: 11
Your score went up by 1 point!
8. Decrement (--)
Decreases a number by 1.
let lives = 5;
lives--;
console.log(lives); // Output: 4
You lost one life in the game!
What are Assignment Operators in JavaScript?
Assignment operators are like little delivery guys in
JavaScript — they take a value and deliver (assign) it to a variable.
They are used when you want to store, update, or change
the value inside a variable.
The most basic assignment operator is =, but there are
several others that help you combine assignment with operations like addition,
subtraction, multiplication, and more.
Here’s a breakdown of important assignment operators:
1. Basic Assignment (=)
It simply assigns a value to a variable.
let age = 25;
Here, 25 is assigned to the variable age.
2. Add and Assign (+=)
Adds a value to the variable and updates it.
let score = 10;
score += 5; // Same as score = score + 5;
console.log(score); // Output: 15
You earned 5 extra points — nice!
3. Subtract and Assign (-=)
Subtracts a value from the variable and updates it.
let money = 100;
money -= 30; // Same as money = money - 30;
console.log(money); // Output: 70
You spent 30 bucks — now 70 are left.
4. Multiply and Assign (*=)
Multiplies the variable by a value and updates it.
let apples = 4;
apples *= 3; // Same as apples = apples * 3;
console.log(apples); // Output: 12
Now you have a lot more apples!
5. Divide and Assign (/=)
Divides the variable by a value and updates it.
let pieces = 20;
pieces /= 4; // Same as pieces = pieces / 4;
console.log(pieces); // Output: 5
Each of 4 friends gets 5 pieces.
6. Modulus and Assign (%=)
Takes the remainder and assigns it back to the variable.
let candies = 17;
candies %= 5; // Same as candies = candies % 5;
console.log(candies); // Output: 2
After sharing candies, 2 are still with you!
7. Exponent and Assign (**=)
Raises the variable to the power of a value and updates
it.
let num = 3;
num **= 2; // Same as num = num ** 2;
console.log(num); // Output: 9
3 raised to the power of 2 is 9!
What are Comparison Operators in JavaScript?
Comparison operators are like the judges of JavaScript. 👩⚖️👨⚖️
They compare two values and decide if they are equal, not equal, greater, smaller,
and so on.
And based on that, they give an answer:
- true
(✅) if the comparison is correct
- false
(❌) if the comparison is wrong
These are super important when you're making decisions in
your code — like checking if a user entered the right password, if a player
scored enough points, or if it’s time to end a game.
Main Comparison Operators:
1. Equal to (==)
Checks if two values are equal, but it doesn’t
care about the type (like string vs number).
console.log(5 == '5'); // true
(5 and "5" are considered equal because ==
ignores type.)
2. Strictly Equal to (===)
Checks if two values are equal AND of the same
type.
(More strict and careful!)
console.log(5 === '5'); // false
console.log(5 === 5); // true
(5 and "5" are NOT the same type.)
3. Not Equal to (!=)
Checks if two values are NOT equal (ignores type).
console.log(10 != '10'); // false
console.log(10 != 5); // true
4. Strictly Not Equal to (!==)
Checks if two values are NOT equal OR not the same
type.
console.log(7 !== '7'); // true
console.log(7 !== 7); // false
5. Greater than (>)
Checks if the first value is bigger than the
second.
console.log(8 > 5); // true
console.log(3 > 10); // false
6. Less than (<)
Checks if the first value is smaller than the
second.
console.log(4 < 9); // true
console.log(12 < 7); // false
7. Greater than or equal to (>=)
Checks if the first value is bigger or equal to
the second.
console.log(10 >= 10); // true
console.log(15 >= 5); // true
8. Less than or equal to (<=)
Checks if the first value is smaller or equal to
the second.
console.log(5 <= 5); // true
console.log(2 <= 8); // true
What are Logical Operators in JavaScript?
In JavaScript, logical operators are like traffic
controllers — they help you decide what
should happen based on true or false values.
They are used to:
- Combine
multiple conditions together
- Invert
(flip) a condition
- Control
the flow of decisions in your programs
Super important when you're writing if conditions, while
loops, form validations, games, etc!
Here are the main Logical Operators:
1. Logical AND (&&)
🔵 Meaning:
Both conditions must be true for the result to be true.
let isAdult = true;
let hasID = true;
if (isAdult && hasID) {
console.log("You can enter the club!");
}
Here, you can enter only if you are an adult AND you have
an ID.
Condition 1 |
Condition 2 |
Result |
true |
true |
true |
true |
false |
false |
false |
true |
false |
false |
false |
false |
2. Logical OR (||)
🔵 Meaning:
If at least one condition is true, the result is true.
let isHoliday = false;
let isWeekend = true;
if (isHoliday || isWeekend) {
console.log("You can relax today!");
}
You can relax if it's a holiday OR a weekend — lucky you!
Condition 1 |
Condition 2 |
Result |
true |
true |
true |
true |
false |
true |
false |
true |
true |
false |
false |
false |
3. Logical NOT (!)
🔵 Meaning:
It reverses the value.
- Turns
true into false
- Turns
false into true
let isRaining = false;
if (!isRaining) {
console.log("Go out and enjoy the sunshine!");
}
Since it's not raining (!false → true), you can go
outside.
Real Life Examples
Login System:
You can log in if your username AND password are correct (&&).
Coupon Offer:
You get a discount if you are a new customer OR if you have a coupon code (||).
Game Rule:
You can't move to the next level if you didn't complete the current one (!).
What are Ternary Operators in JavaScript?
The ternary operator is a shorthand for the if-else
statement. It allows you to write a conditional expression in a compact form.
Basically, the ternary operator allows you to ask a
question and give a result depending on whether the answer is true or false.
The syntax looks like this:
condition ? value_if_true : value_if_false;
How Does It Work?
- condition:
The expression you want to test (like checking if a number is greater than
10).
- value_if_true:
What happens if the condition is true.
- value_if_false:
What happens if the condition is false.
Example 1: Basic Ternary Operator
Let’s say you want to check if a person is old enough to
drive. If they are, they can drive, otherwise, they can’t.
let age = 20;
let canDrive = age >= 18 ? "You can drive!" : "You cannot drive.";
console.log(canDrive); // Output: "You can drive!"
- If
age >= 18 is true, it prints "You can drive!".
- If
age >= 18 is false, it prints "You cannot drive.".
Example 2: Using Ternary with Multiple Conditions
You can also nest ternary operators or use them for
multiple conditions, though it's often better to keep things readable.
let time = 10;
let greeting = time < 12 ? "Good Morning!" : time < 18 ? "Good Afternoon!" : "Good Evening!";
console.log(greeting); // Output: "Good Morning!"
Here, depending on the value of time, it will print:
- "Good
Morning!" if it's before noon,
- "Good
Afternoon!" if it's after noon but before 6 PM,
- "Good
Evening!" if it's after 6 PM.
Example 3: Assigning Values with Ternary
You can use a ternary operator to assign a value to a
variable based on a condition.
let isEvening = true;
let message = isEvening ? "Time for dinner!" : "It’s not dinner time yet.";
console.log(message); // Output: "Time for dinner!"
What are Bitwise Operators ?
Bitwise operators deal with the bits (0s and 1s) of
numbers. They’re mainly used for low-level programming or when you need to
manipulate data at the binary level. But you don’t need to worry about the
super technical stuff — just understand how they work on numbers!
Here are the common bitwise operators:
- &
(AND): Compares bits of two numbers. The result is 1 if both bits are 1.
- |
(OR): Compares bits. The result is 1 if at least one bit is 1.
- ^
(XOR): Compares bits. The result is 1 if one bit is different from the
other (but not both).
- ~
(NOT): Flips all bits (turns 1s into 0s and 0s into 1s).
- <<
(Left Shift): Shifts bits to the left (multiplies the number by 2 for each
shift).
- >>
(Right Shift): Shifts bits to the right (divides the number by 2 for each
shift).
Example:
let a = 5; // In binary: 101
let b = 3; // In binary: 011
console.log(a & b); // Output: 1 (binary: 001)
console.log(a | b); // Output: 7 (binary: 111)
console.log(a ^ b); // Output: 6 (binary: 110)
What are Type Operators ?
Type operators are used to check the type of a variable
or expression. They are very handy when you want to know the type of something
in your program!
Common Type Operators:
- typeof:
Returns the type of a variable (string, number, object, etc.).
- instanceof:
Checks if an object is an instance of a specific class or constructor
function.
Example:
let age = 25;
console.log(typeof age); // Output: "number"
let user = new Date();
console.log(user instanceof Date); // Output: true
- typeof
tells you the type of age, which is a number.
- instanceof
checks if user is an instance of the Date class.
What are string Operators ?
In JavaScript, strings are sequences of characters
(text). There are a couple of operators specifically for working with strings.
Common String Operators:
- +
(Concatenation): Combines two strings together.
- +=
(Concatenation Assignment): Adds to the end of a string.
Example:
let firstName = "John";
let lastName = "Doe";
// Concatenation using +
let fullName = firstName + " " + lastName;
console.log(fullName); // Output: "John Doe"
// Concatenation using +=
let message = "Hello, ";
message += "world!";
console.log(message); // Output: "Hello, world!"
The + operator adds the strings together, and += appends
a string to the existing one.
What are Comma Operator ?
The comma operator allows you to evaluate multiple
expressions, but only returns the value of the last expression. This can be
useful when you want to perform several tasks on one line.
let a = 5;
let b = 10;
let c = (a++, b++, a + b); // a++ and b++ happen first, then a + b is evaluated
console.log(c); // Output: 17 (a becomes 6, b becomes 11)
Here:
- The
comma operator allows both a++ and b++ to be executed first.
- Then,
the value of a + b is returned (which is 17).
Final Thoughts
Mastering JavaScript operators is a vital step in writing
clean, effective code. They are the foundation for performing calculations,
making decisions, and controlling logic in your programs.
Now that you’ve learned
the basics, get hands-on! Try using them in your browser's console or create
simple projects like a calculator or form validator to practice.
Have any questions or need more examples? Drop them in the comments below!