JavaScript Operators: The Basics You Need to Know
Operators are one of the most fundamental parts of programming. In JavaScript, operators allow us to perform calculations, compare values, and make logical decisions in our programs.
In simple terms:
Operators are symbols that perform operations on values (operands).
For example:
5 + 3
Here:
5and3→ operands+→ operator
Let’s explore the most commonly used operators in JavaScript.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Meaning | 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);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
Output:
13
7
30
3.33
1
The % operator returns the remainder after division.
Comparison Operators
Comparison operators are used to compare two values. They return a boolean value (true or false).
| Operator | Meaning |
|---|---|
| == | Equal to |
| === | Strict equal |
| != | Not equal |
| > | Greater than |
| < | Less than |
Example:
let x = 10;
let y = "10";
console.log(x == y);
console.log(x === y);
Output:
true
false
Difference Between == and ===
| Operator | Behavior |
|---|---|
| == | Compares values only |
| === | Compares value AND type |
Example:
10 == "10" // true
10 === "10" // false
Because "10" is a string while 10 is a number.
For safer code, developers usually prefer ===.
Logical Operators
Logical operators are used to combine conditions.
| Operator | Meaning |
|---|---|
| && | AND |
| ! | NOT |
Example:
let age = 20;
let hasID = true;
if (age >= 18 && hasID) {
console.log("Allowed to enter");
}
Output:
Allowed to enter
Logical Operator Truth Table
| A | B | A && B | A || B |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
The ! operator simply reverses a value.
Example:
let isLoggedIn = false;
console.log(!isLoggedIn);
Output:
true
Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Meaning |
|---|---|
| = | Assign value |
| += | Add and assign |
| -= | Subtract and assign |
Example:
let score = 10;
score += 5;
console.log(score);
score -= 3;
console.log(score);
Output:
15
12
Here:
score += 5 → score = score + 5
score -= 3 → score = score - 3
Small Practice Example
Let’s combine multiple operators in a simple program.
let num1 = 15;
let num2 = 5;
console.log("Addition:", num1 + num2);
console.log("Multiplication:", num1 * num2);
console.log(num1 == num2);
console.log(num1 > num2);
if (num1 > 10 && num2 < 10) {
console.log("Condition is true");
}
Output:
Addition: 20
Multiplication: 75
false
true
Condition is true
Operator Categories (Quick Overview)
| Category | Examples |
|---|---|
| Arithmetic | +, -, *, /, % |
| Comparison | ==, ===, !=, >, < |
| Logical | &&, |
| Assignment | =, +=, -= |
Conclusion
Operators are essential for performing calculations and making decisions in JavaScript programs.
Key takeaways:
Arithmetic operators perform mathematical operations.
Comparison operators compare values and return
trueorfalse.Logical operators combine conditions.
Assignment operators store and update values in variables.
By understanding these basic operators, you can start writing more powerful and dynamic JavaScript programs.