JavaScript operators are special symbols used to perform operations on variables, values, and expressions. They allow developers to calculate values, compare data, assign information, and make logical decisions within a program. Every JavaScript application, from a simple calculator to a complex web application, relies on operators to process data efficiently.
Whenever you add two numbers, compare two values, assign a value to a variable, or check whether a condition is true or false, you are using JavaScript operators. Understanding operators is one of the most important steps in learning JavaScript because they are used in almost every program.
For example, when a shopping website calculates the total bill, when a login page checks a password, or when a game increases a player's score, JavaScript operators perform these operations behind the scenes.
A JavaScript operator is a symbol or keyword that performs a specific operation on one or more values called operands. The result of the operation is returned as a new value.
In the following example, the plus (+) symbol is an arithmetic operator that adds two numbers.
operand operator operand
let a = 15; let b = 5; let result = a + b; console.log(result);
20
Here, a and b are operands, while + is the operator.
Operators help JavaScript perform calculations, manipulate data, compare values, and execute logical decisions. Without operators, writing useful programs would not be possible.
JavaScript provides several categories of operators, each designed for a different purpose.
| Operator Type | Purpose |
|---|---|
| Arithmetic Operators | Perform mathematical calculations. |
| Assignment Operators | Assign values to variables. |
| Comparison Operators | Compare two values. |
| Logical Operators | Combine multiple conditions. |
| Bitwise Operators | Perform operations on binary values. |
| String Operators | Combine string values. |
| Ternary Operator | Write short conditional expressions. |
| Type Operators | Determine or check data types. |
Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, division, and finding remainders. These are among the most frequently used operators in JavaScript.
| Operator | Name | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b |
| ** | Exponentiation | a ** b |
The addition operator adds two numeric values and returns their sum.
let x = 20; let y = 15; console.log(x + y);
35
The subtraction operator subtracts one value from another.
let marks = 90; let deduction = 8; console.log(marks - deduction);
82
The multiplication operator multiplies two numbers.
let price = 150; let quantity = 4; console.log(price * quantity);
600
The division operator divides one value by another and returns the quotient.
let total = 100; let students = 5; console.log(total / students);
20
The modulus operator returns the remainder after division. It is commonly used to determine whether a number is even or odd.
let number = 17; console.log(number % 2);
1
Since the remainder is 1, the number is odd.
The exponentiation operator raises a number to the power of another number.
let result = 3 ** 4; console.log(result);
81
The increment operator increases the value of a variable by one. It is commonly used inside loops and counters.
let count = 10; count++; console.log(count);
11
The decrement operator decreases the value of a variable by one.
let score = 25; score--; console.log(score);
24
let a = 12;
let b = 4;
console.log("Addition:", a + b);
console.log("Subtraction:", a - b);
console.log("Multiplication:", a * b);
console.log("Division:", a / b);
console.log("Remainder:", a % b);
console.log("Power:", a ** b);
Addition: 16 Subtraction: 8 Multiplication: 48 Division: 3 Remainder: 0 Power: 20736
In the next section, you will learn about Assignment Operators, Comparison Operators, Logical Operators, and their practical applications with real-world examples.
Assignment operators are used to assign values to variables. They are commonly used to store data and update existing variable values. Besides the simple assignment operator, JavaScript provides several compound assignment operators that perform a calculation and assignment in a single statement.
Using assignment operators makes code shorter, cleaner, and easier to maintain. They are frequently used in loops, counters, calculations, and real-world web applications.
| Operator | Description | Example |
|---|---|---|
| = | Assigns a value | x = 10 |
| += | Add and assign | x += 5 |
| -= | Subtract and assign | x -= 2 |
| *= | Multiply and assign | x *= 4 |
| /= | Divide and assign | x /= 2 |
| %= | Assign remainder | x %= 3 |
| **= | Assign exponentiation | x **= 2 |
The assignment operator stores a value inside a variable. It is the most frequently used operator in JavaScript programming.
let age = 20; console.log(age);
20
The += operator adds a value to the current variable and stores the updated result in the same variable.
let marks = 80; marks += 10; console.log(marks);
90
The -= operator subtracts a value from a variable and updates it immediately.
let balance = 500; balance -= 120; console.log(balance);
380
This operator multiplies the existing value by another number and saves the result.
let salary = 25000; salary *= 2; console.log(salary);
50000
The /= operator divides the current value by another number and stores the result.
let total = 100; total /= 5; console.log(total);
20
Comparison operators compare two values and always return either true or false. These operators are mainly used in conditional statements such as if, else, loops, and decision-making programs.
| Operator | Description | Example |
|---|---|---|
| == | Equal to | 10 == 10 |
| === | Strict equal | 10 === "10" |
| != | Not equal | 10 != 5 |
| !== | Strict not equal | 10 !== "10" |
| > | Greater than | 20 > 15 |
| < | Less than | 5 < 10 |
| >= | Greater than or equal | 18 >= 18 |
| <= | Less than or equal | 12 <= 20 |
The equality operator compares only the values after automatic type conversion.
console.log(20 == "20");
true
Although one value is a number and the other is a string, JavaScript converts the string into a number before comparison.
The strict equality operator compares both the value and the data type. It does not perform automatic type conversion.
console.log(20 === "20");
false
Since the data types are different, the result is false.
let age = 22; console.log(age > 18); console.log(age < 18);
true false
Logical operators combine multiple conditions and return a Boolean result. They are commonly used in login systems, validation forms, search filters, and decision-making applications.
| Operator | Name | Description |
|---|---|---|
| && | Logical AND | Returns true only if all conditions are true. |
| || | Logical OR | Returns true if at least one condition is true. |
| ! | Logical NOT | Reverses the Boolean value. |
let age = 20; let citizen = true; console.log(age >= 18 && citizen);
true
Both conditions are true, so the final result is true.
let marks = 45; let sportsQuota = true; console.log(marks >= 50 || sportsQuota);
true
Only one condition needs to be true when using the OR operator.
let isLoggedIn = false; console.log(!isLoggedIn);
true
let username = "admin";
let password = "12345";
if(username == "admin" && password == "12345")
{
console.log("Login Successful");
}
else
{
console.log("Invalid Credentials");
}
Login Successful
In the next section, you will learn about Bitwise Operators, String Operators, Type Operators, Ternary Operator, Operator Precedence, and JavaScript operator best practices with practical examples.
Bitwise operators perform operations directly on the binary (0 and 1) representation of numbers. Although they are not used as frequently as arithmetic or logical operators, they are useful in system programming, graphics, encryption, data compression, and performance optimization.
Before applying a bitwise operator, JavaScript converts decimal numbers into binary form, performs the operation, and converts the result back into a decimal number.
| Operator | Name | Description |
|---|---|---|
| & | Bitwise AND | Returns 1 only if both bits are 1. |
| | | Bitwise OR | Returns 1 if either bit is 1. |
| ^ | Bitwise XOR | Returns 1 when bits are different. |
| ~ | Bitwise NOT | Inverts every bit. |
| << | Left Shift | Moves bits to the left. |
| >> | Right Shift | Moves bits to the right. |
let x = 6; let y = 3; console.log(x & y);
2
The plus (+) operator can also combine two or more strings. This process is called string concatenation.
let firstName = "CSE"; let lastName = "Gyan"; let fullName = firstName + " " + lastName; console.log(fullName);
CSE Gyan
Type operators help determine the type of a variable or check whether an object belongs to a particular class.
The typeof operator returns the data type of a variable or value.
let city = "Delhi"; console.log(typeof city); console.log(typeof 50); console.log(typeof true);
string number boolean
The instanceof operator checks whether an object is created from a specific constructor.
let numbers = [10,20,30]; console.log(numbers instanceof Array);
true
The ternary operator is a short and efficient way to write simple conditional statements. It uses three parts: a condition, the value returned if the condition is true, and the value returned if the condition is false.
condition ? value1 : value2;
let age = 19; let result = age >= 18 ? "Eligible" : "Not Eligible"; console.log(result);
Eligible
When multiple operators appear in the same expression, JavaScript follows operator precedence rules to determine which operation should be performed first.
let result = 10 + 5 * 2; console.log(result);
20
Multiplication has a higher precedence than addition, so 5 × 2 is calculated first, followed by adding 10.
let result = (10 + 5) * 2; console.log(result);
30
let itemPrice = 800;
let quantity = 3;
let discount = 200;
let total = itemPrice * quantity;
let finalAmount = total - discount;
console.log("Total Price:", total);
console.log("Final Amount:", finalAmount);
console.log(finalAmount > 2000 ? "Free Delivery" : "Delivery Charges Apply");
Total Price: 2400 Final Amount: 2200 Free Delivery
JavaScript operators are essential tools for performing calculations, assigning values, comparing data, and making decisions in programs. They help developers write efficient and readable code while solving real-world problems. Understanding arithmetic, assignment, comparison, logical, bitwise, string, type, and ternary operators provides a strong foundation for learning advanced JavaScript concepts.
As you continue learning JavaScript, you will use these operators in loops, functions, DOM manipulation, form validation, event handling, and nearly every interactive web application you build.