JavaScript Operators: The Basics You Need to Know

Introduction
In JavaScript, operators are symbols used to perform operations on values and variables. They allow us to manipulate data, perform calculations, compare values, and make logical decisions in our programs.
For example:
let sum = 5 + 3;
Here, + is an operator that adds two numbers.
Operators are essential in programming because they help us write logic, perform calculations, and control how programs behave.
In this article, we will explore the most commonly used JavaScript operators, including arithmetic, comparison, logical, and assignment operators.
1. What Operators Are
An operator is a symbol that performs an operation on one or more values.
These values are called operands.
Example:
let result = 10 + 5;
Explanation:
| Part | Meaning |
|---|---|
| 10 | Operand |
| + | Operator |
| 5 | Operand |
The operator performs an operation on the operands.
Operators in JavaScript help with:
Performing calculations
Comparing values
Assigning values
Building logical conditions
2. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Common Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ |
Addition | 5 + 3 |
- |
Subtraction | 10 - 4 |
* |
Multiplication | 6 * 2 |
/ |
Division | 8 / 2 |
% |
Modulus (remainder) | 10 % 3 |
Example
let a = 10;
let b = 3;
console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.33
console.log(a % b); // 1
The modulus operator % returns the remainder after division.
Example:
10 % 3 = 1
3. Comparison Operators
Comparison operators compare two values and return a boolean value (true or false).
Common Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | 5 == "5" |
=== |
Strict equal | 5 === "5" |
!= |
Not equal | 5 != 3 |
> |
Greater than | 10 > 5 |
< |
Less than | 3 < 8 |
Example
let a = 5;
let b = "5";
console.log(a == b); // true
console.log(a === b); // false
Explanation:
==compares values but ignores data type===compares both value and type
Example:
5 == "5" → true
5 === "5" → false
Using === is generally recommended because it avoids unexpected type conversions.
4. Logical Operators
Logical operators are used to combine multiple conditions.
They are commonly used in if statements and control flow.
Logical Operators
| Operator | Meaning |
|---|---|
&& |
Logical AND |
| ! | Logical NOT |
Logical AND (&&)
Returns true only if both conditions are true.
Example:
let age = 20;
if (age > 18 && age < 30) {
console.log("Young adult");
}
Both conditions must be true.
Logical OR (||)
Returns true if at least one condition is true.
Example:
let role = "admin";
if (role === "admin" || role === "editor") {
console.log("Access granted");
}
Logical NOT (!)
Reverses the boolean value.
Example:
let isLoggedIn = false;
console.log(!isLoggedIn); // true
5. Assignment Operators
Assignment operators are used to assign values to variables.
Common Assignment Operators
| Operator | Example | Meaning |
|---|---|---|
= |
x = 5 |
Assign value |
+= |
x += 3 |
Add and assign |
-= |
x -= 2 |
Subtract and assign |
Example
let x = 10;
x += 5;
console.log(x); // 15
This is the same as:
x = x + 5;
Another example:
let y = 20;
y -= 5;
console.log(y); // 15
Which is equivalent to:
y = y - 5;
Conclusion
Operators are a fundamental part of JavaScript and are used to perform operations on values and variables. They allow developers to perform calculations, compare data, combine conditions, and assign values efficiently.
In this article, we explored the basics of JavaScript operators, including arithmetic operators for calculations, comparison operators for evaluating values, logical operators for combining conditions, and assignment operators for updating variables.
Understanding these operators is essential for writing logical and efficient JavaScript programs. As you continue learning JavaScript, you will frequently use these operators when building real-world applications