What are Operators in JavaScript?
Operators in JavaScript are special symbols used to perform operations on operands (values or variables). Think of them as tools that help you calculate, compare, assign, or manipulate data in your program.
Types of JavaScript Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
- Ternary Operator
- Type Operators
Arithmetic Operators
These operators perform basic mathematical operations:
+
Addition-
Subtraction*
Multiplication/
Division%
Modulus (Remainder)**
Exponentiation (Power)
Example:
let a = 10;
let b = 3;
console.log("Addition:", a + b);
console.log("Subtraction:", a - b);
console.log("Multiplication:", a * b);
console.log("Division:", a / b);
console.log("Modulus:", a % b);
console.log("Exponentiation:", a ** b);
Output:
Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3.3333333333333335 Modulus: 1 Exponentiation: 1000
Assignment Operators
These assign values to variables. You can also use them to perform operations and assign the result in one step:
=
Assign+=
Add and assign-=
Subtract and assign*=
Multiply and assign/=
Divide and assign%=
Modulus and assign
Example:
let x = 5;
x += 3; // x = x + 3
console.log("After += 3:", x);
x *= 2; // x = x * 2
console.log("After *= 2:", x);
Output:
After += 3: 8 After *= 2: 16
Comparison Operators
Used to compare two values. Returns a Boolean value (true/false):
==
Equal to (loose equality)===
Equal value and type (strict equality)!=
Not equal (loose)!==
Not equal (strict)>
Greater than<
Less than>=
Greater than or equal<=
Less than or equal
Example:
console.log(5 == "5"); // true (type coercion)
console.log(5 === "5"); // false (different types)
console.log(10 > 7); // true
console.log(3 <= 2); // false
Output:
true false true false
Logical Operators
Used to combine multiple conditions:
&&
Logical AND||
Logical OR!
Logical NOT
Example:
let isLoggedIn = true;
let hasPermission = false;
console.log("Can access?", isLoggedIn && hasPermission);
console.log("Partial access?", isLoggedIn || hasPermission);
console.log("Not logged in?", !isLoggedIn);
Output:
Can access? false Partial access? true Not logged in? false
Unary Operators
Works with only one operand.
+
Unary plus (converts to number)-
Unary minus (negates)++
Increment--
Decrementtypeof
Returns the data type
Example:
let y = "5";
console.log(typeof +y); // number
let count = 1;
console.log(++count); // 2
Output:
number 2
Ternary Operator
This is a shortcut for if-else
condition. Syntax: condition ? valueIfTrue : valueIfFalse
Example:
let age = 20;
let status = age >= 18 ? "Adult" : "Minor";
console.log("Status:", status);
Output:
Status: Adult
Type Operators
typeof
- Returns a string indicating the type of the operandinstanceof
- Checks if an object is an instance of a specific class
Example:
let text = "Hello";
console.log(typeof text); // string
let today = new Date();
console.log(today instanceof Date); // true
Output:
string true
Beginner Tip
Q: What’s the difference between ==
and ===
in JavaScript?
A: ==
compares only values (performs type coercion), while ===
compares both value and type. Always prefer ===
to avoid unexpected bugs.
Q: Why use the ternary operator when we already have if-else?
A: Ternary operator is useful for quick decisions or assignments. It makes the code concise but use it wisely to maintain readability.