JavaScript – Operators
1.What are Operators?
An operator is a symbol that tells the JavaScript engine to perform a specific operation on one or more operands (values or variables).
Types of Operators:
- Arithmetic Operator
- Boolean Operator
- Increment & Decrement Operator
- Comparsion Operator
- Conditional Operator
- Logical Operator
Arithmetic Operators
These work with two operands and one operator.
Operators: +, -, , /, %, *.
Examples:
- 10 + 5 → 15
- “10” + 5 → “105” (Concatenation – string + number becomes string)
- “10” – 5 → 5 (JavaScript auto-converts string to number using parseInt)
- 10 * 2 → 20
- 10 / 2 → 5
- 10 % 2 → 0
- 2 ** 3 → 8 (Exponentiation)
Boolean Operators
In JavaScript:
true is treated as 1
false is treated as 0
** Examples:**
- true + 1 → 2
- false – 2 → -2
Increment & Decrement Operators.
** Post-Increment (i++)**
First returns the value, then increments.
example:
let i = 5;
console.log(i++); // Output: 5
Post-Decrement (i–)
First returns the value, then decrements.
example:
let i = 5;
console.log(i–); // Output: 5
Pre-Increment (++i)
First increments the value, then returns it.
example:
let i = 5;
console.log(++i); // Output: 6
Pre-Decrement (–i)
First decrements the value, then returns it.
example:
let i = 5;
console.log(–i); // Output: 4
Comparison Operators
Used to compare values and return a boolean (true or false)
Loose Equality (==)
Compares values, not data types
example:
5 == 5 // true
“5” == 5 // true
Strict Equality (===)
Compares both value and data type
example:
“5” === 5 // false
Other Comparisons:
- > : Greater Than
- < : Less Than
- >= : Greater Than or Equal To
-
<= : Less Than or Equal To
Example: -
7 > 5 // true
-
5 < 7 // true
-
5 <= 5 // true
Logical Operators.
OR (||)
Returns true if any one condition is** true**.
- true || false // true
- false || false // false
** AND (&&)**
Returns true only if both conditions are true
- true && true // true
- true && false // false
NOT (!)
Reverses the boolean value
- !true // false
- !false // true
Final Thoughts:
JavaScript operators are the building blocks of logic in any program. From basic arithmetic to comparing values and making decisions, operators help us write powerful and dynamic code. By practicing each type—arithmetic, comparison, logical, and more—we gain a deeper understanding of how JavaScript thinks and behaves. Mastering them is the first step toward writing clean and efficient code.