Operators are special symbols used to perform operations on operands (values and variables).
For example, in 10 + 5, the + is the operator and 10 and
5 are operands.
Arithmetic operators perform mathematical calculations like addition, subtraction, multiplication, etc.
+ (Addition)- (Subtraction)* (Multiplication)/ (Division)% (Modulus - Remainder)** (Exponentiation)++ (Increment)-- (Decrement)
let a = 10;
let b = 3;
console.log("Addition: " + (a + b)); // 13
console.log("Subtraction: " + (a - b)); // 7
console.log("Multiplication: " + (a * b)); // 30
console.log("Division: " + (a / b)); // 3.333...
console.log("Modulus: " + (a % b)); // 1
console.log("Exponentiation: " + (a ** b));// 1000
Prefix (++x): Increments the value, then returns it.
Postfix (x++): Returns the value, then increments it.
let x = 5;
console.log(x++); // 5 (Returns original value, then increments)
console.log(x); // 6
let y = 5;
console.log(++y); // 6 (Increments, then returns new value)
Assignment operators assign values to JavaScript variables. The most common one is =.
let x = 10; // Assignment
x += 5; // Same as x = x + 5
console.log("x += 5 ->", x); // 15
x -= 2; // Same as x = x - 2
console.log("x -= 2 ->", x); // 13
x *= 2; // Same as x = x * 2
console.log("x *= 2 ->", x); // 26
Comparison operators are used in logical statements to determine equality or difference between variables or values.
== Equal to (value only)=== Equal value and equal type (Strict Equality)!= Not equal!== Not equal value or not equal type> Greater than< Less than
let num = 5;
let str = "5";
console.log(num == str); // true (values match)
console.log(num === str); // false (types differ: number vs string)
console.log(num != 8); // true
console.log(num > 2); // true
Logical operators are used to determine the logic between variables or values.
&& (AND): Returns true if both operands are true.|| (OR): Returns true if one of the operands is true.! (NOT): Reverses the boolean result.
let x = 6;
let y = 3;
console.log(x < 10 && y > 1); // true (both true)
console.log(x == 5 || y == 5); // false (neither is true)
console.log(!(x == y)); // true (inverse of false)
A shorthand for the if...else statement.
condition ? value_if_true : value_if_false
let age = 20;
let type = (age >= 18) ? "Adult" : "Minor";
console.log(type);
The typeof operator returns the type of a variable or an expression.
console.log(typeof "John"); // string
console.log(typeof 3.14); // number
console.log(typeof true); // boolean
console.log(typeof [1,2]); // object (arrays are objects)
console.log(typeof {x:1}); // object
| Feature | == (Loose Equality) | === (Strict Equality) |
|---|---|---|
| Compares | Only Values | Values AND Data Types |
| Type Coercion | Yes (e.g., "10" becomes 10) | No |
| Example | 10 == "10" | 10 === "10" |
| Result | true | false |
Test your understanding of JavaScript Operators.
👉 Find: Gross Salary (basic + HRA + DA), Total Salary (after bonus), and Final Salary (after tax deduction).