Introduction
If you have ever used a calculator, you already know what an operator is. When you type 2 + 3, the + symbol tells the calculator to perform addition.
In programming, an operator is a special symbol that performs a specific operation on one or more values (called operands). For example, in 2 + 3, the numbers 2 and 3 are the operands, and + is the operator.
JavaScript provides many different types of operators to help us perform mathematics, assign values, compare data, and make logical decisions.
In this article, we will cover:
- What operators are
- Arithmetic Operators (Unary and Binary)
- Assignment Operators
- Comparison Operators (Strict vs. Loose Equality)
- Logical Operators
- Ternary Operator
- Conditionals (
if,else if,else, andswitch)
1. Types of Operators in JavaScript
JavaScript operators can be grouped into several categories based on what they do:
- Arithmetic Operators: Used for mathematical calculations.
- Assignment Operators: Used to assign values to variables.
- Comparison Operators: Used to compare two values.
- Logical Operators: Used to combine multiple conditions.
- Ternary Operator: A shorthand way to write simple
if-elseconditions. - Bitwise Operators: Used for binary level operations (advanced, we will skip this for now).
Let's explore the most important ones.
2. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations. They can be divided into two types: Binary and Unary.
Binary Operators
Binary operators require two operands.
let sum = 10 + 5; // Addition (+) -> 15
let difference = 10 - 5; // Subtraction (-) -> 5
let product = 10 * 5; // Multiplication (*) -> 50
let quotient = 10 / 5; // Division (/) -> 2
let remainder = 10 % 3; // Modulus (Remainder) (%) -> 1
let power = 2 ** 3; // Exponentiation (**) -> 8
Unary Operators
Unary operators require only one operand. The most common are Increment (++) and Decrement (--).
- Increment (
++): Increases a value by 1. - Decrement (
--): Decreases a value by 1.
These can be used as Prefix (before the variable) or Postfix (after the variable).
let a = 5;
// Post-increment: Use the value first, then increase it.
console.log(a++); // Output: 5
console.log(a); // Output: 6
let b = 5;
// Pre-increment: Increase the value first, then use it.
console.log(++b); // Output: 6
console.log(b); // Output: 6
The same logic applies to pre-decrement (--a) and post-decrement (a--).
3. Assignment Operators
Assignment operators are used to assign a value to a variable. The most common is the equals sign =.
let a = 5;
Here, = is the assignment operator. It takes the value 5 and stores it in the memory location associated with the variable a.
Shorthand Assignment Operators
JavaScript provides shorthand ways to perform an arithmetic operation and assign the result at the same time.
let score = 10;
score += 5; // Same as: score = score + 5 (score becomes 15)
score -= 2; // Same as: score = score - 2 (score becomes 13)
score *= 2; // Same as: score = score * 2 (score becomes 26)
score /= 2; // Same as: score = score / 2 (score becomes 13)
4. Comparison Operators
Comparison operators compare two values and return a Boolean result: true or false.
console.log(10 > 5); // Greater than: true
console.log(10 < 5); // Less than: false
console.log(10 >= 10); // Greater than or equal to: true
console.log(5 <= 10); // Less than or equal to: true
Strict Equality (===) vs. Loose Equality (==)
This is a very common interview question!
- Loose Equality (
==): Compares only the values. If the data types are different, JavaScript will try to convert them to be the same before comparing. - Strict Equality (
===): Compares both the values AND the data types. No conversion happens.
console.log(5 == "5"); // true (Values are the same, type is ignored)
console.log(5 === "5"); // false (Values are the same, but Number is not a String)
Tip: Always use Strict Equality (
===) and Strict Inequality (!==) to avoid unexpected bugs in your code!
5. Logical Operators
Logical operators are used to merge multiple conditions together.
AND (&&)
Returns true ONLY if all conditions are true. If even one condition is false, the result is false.
let age = 20;
let hasID = true;
if (age >= 18 && hasID === true) {
console.log("You can enter.");
}
// Output: You can enter.
OR (||)
Returns true if at least one condition is true. It only returns false if all conditions are false.
let isWeekend = true;
let isHoliday = false;
if (isWeekend || isHoliday) {
console.log("No school today!");
}
// Output: No school today!
NOT (!)
Reverses the boolean value. If it's true, it becomes false. If it's false, it becomes true.
let isRaining = false;
console.log(!isRaining); // Output: true
6. Conditionals (Making Decisions)
Operators are powerful because they allow us to write Conditionals. Conditionals tell the computer to execute certain code only if a specific condition is met.
The if, else if, and else Statements
let trafficLight = "red";
if (trafficLight === "green") {
console.log("Go!");
} else if (trafficLight === "yellow") {
console.log("Slow down.");
} else {
console.log("Stop!");
}
// Output: Stop!
if: Runs if the condition is true.else if: Runs if the first condition was false, but this new one is true.else: Runs if all previous conditions were false.
The switch Statement
When you have many else if conditions checking the same variable, a switch statement is much cleaner.
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
// Output: Wednesday
7. The Ternary Operator
The ternary operator is a shortcut for a simple if-else statement. It takes three operands:
- The condition
- The result if true
- The result if false
Syntax:
condition ? result_if_true : result_if_false;
Example: Imagine you are checking if someone is old enough to vote.
Using an if-else statement:
let age = 20;
let canVote;
if (age >= 18) {
canVote = "Yes, you can vote.";
} else {
canVote = "No, you cannot vote.";
}
Using the Ternary Operator:
let age = 20;
let canVote = (age >= 18) ? "Yes, you can vote." : "No, you cannot vote.";
console.log(canVote);
// Output: Yes, you can vote.
8. Bitwise Operators
Bitwise operators treat their operands as a set of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. They perform operations on these binary representations, but they return standard JavaScript numerical values.
While you might not use them every day as a beginner, they are very powerful for low-level programming, graphics, and optimizing certain algorithms.
Here are the most common bitwise operators:
- AND (
&): Returns a 1 in each bit position for which the corresponding bits of both operands are 1. - OR (
|): Returns a 1 in each bit position for which the corresponding bits of either or both operands are 1. - XOR (
^): Returns a 1 in each bit position for which the corresponding bits of either but not both operands are 1. - NOT (
~): Inverts the bits of its operand. - Left Shift (
<<): Shifts the bits of the first operand to the left by the number of places specified by the second operand. - Sign-propagating Right Shift (
>>): Shifts the bits of the first operand to the right.
Example:
let a = 5; // Binary: 0101
let b = 1; // Binary: 0001
console.log(a & b); // Output: 1 (Binary: 0001)
console.log(a | b); // Output: 5 (Binary: 0101)
Summary
- Arithmetic Operators (
+,-,*,/,%) perform math. - Assignment Operators (
=,+=,*=) store values in variables. - Comparison Operators (
>,<,===) return true or false. Always use===. - Logical Operators (
&&,||,!) combine multiple conditions. - Conditionals (
if,else,switch) control the flow of your program. - Ternary Operator (
? :) is a clean shorthand for simpleif-elselogic.
By mastering these operators and conditionals, you now have the tools to write complex logic and build interactive JavaScript applications!





