JavaScript operators are the building blocks of every JavaScript program. They allow you to perform calculations, compare values, assign data, and control program logic. Whether you're creating a simple calculator or a complex web application, understanding operators is essential.
In this guide, you'll learn the most commonly used JavaScript operators with easy-to-understand examples.
What Are JavaScript Operators?
A JavaScript operator is a special symbol that operates on one or more values (called operands).
For example:
let sum = 10 + 5;
console.log(sum);
Output:
15
Here, the + symbol is an operator that adds two numbers.
Types of JavaScript Operators
JavaScript provides several categories of operators.
- Arithmetic Operators
Arithmetic operators perform mathematical calculations.
Operator Description Example
- Addition 10 + 5
- Subtraction 10 - 5
- Multiplication 10 * 5 / Division 10 / 5 % Modulus 10 % 3 ** Exponentiation 2 ** 3 Example let a = 20; let b = 4;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
- Assignment Operators
Assignment operators store values in variables.
let x = 10;
x += 5;
x -= 2;
x *= 3;
x /= 2;
console.log(x);
Common assignment operators include:
=
+=
-=
*=
/=
%=
- Comparison Operators
Comparison operators compare two values and return either true or false.
let a = 15;
let b = 20;
console.log(a == b);
console.log(a != b);
console.log(a > b);
console.log(a < b);
console.log(a >= b);
console.log(a <= b);
- Logical Operators
Logical operators combine multiple conditions.
AND (&&)
let age = 22;
console.log(age > 18 && age < 30);
Output:
true
OR (||)
let score = 45;
console.log(score > 50 || score == 45);
Output:
true
NOT (!)
console.log(!(10 > 5));
Output:
false
- String Operator
The + operator can also join strings.
let firstName = "John";
let lastName = "Doe";
console.log(firstName + " " + lastName);
Output:
John Doe
- Ternary Operator
The ternary operator is a short form of an if...else statement.
let age = 19;
let result = age >= 18 ? "Adult" : "Minor";
console.log(result);
Output:
Adult
- Type Operator
The typeof operator tells you the data type of a variable.
let language = "JavaScript";
console.log(typeof language);
console.log(typeof 100);
console.log(typeof true);
Output:
string
number
boolean
Real-Life Example
Suppose you're building a shopping website.
let price = 1000;
let discount = 200;
let finalPrice = price - discount;
console.log(finalPrice);
Output:
800
Here, arithmetic and assignment operators work together to calculate the final amount.
Best Practices
Use === instead of == whenever possible for more predictable comparisons.
Write meaningful variable names.
Use parentheses to make complex expressions easier to read.
Keep your calculations simple and readable.
Test logical conditions carefully before deploying your application.
Common Mistakes
Confusing = with ==.
Using == when === is the better choice.
Forgetting operator precedence in long expressions.
Mixing strings and numbers unintentionally.
*Conclusion
*
JavaScript operators are fundamental tools that make programming possible. From performing mathematical calculations to checking conditions and manipulating data, operators are used in almost every JavaScript application. Once you become comfortable with these operators, writing clean, efficient, and interactive JavaScript code becomes much easier.
Top comments (0)