Operators in JavaScript are special symbols used to perform operations on operands. They are categorized into several types based on their functionality.
Types of Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- String Operators
- Conditional (Ternary) Operator
- Unary Operators
- Comma Operator
1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic on numbers.
- Addition (+)
let x = 5 + 2; // x = 7
- Subtraction (-)
let x = 5 – 2; // x = 3
- Multiplication (*)
let x = 5 * 2; // x = 10
- Division (/)
let x = 5 / 2; // x = 2.5
- Modulus (%) (Remainder)
let x = 5 % 2; // x = 1
- Exponentiation (**)
let x = 5 ** 2; // x = 25
- Increment (++)
let x = 5; x++; // x = 6
- Decrement (–)
let x = 5; x–; // x = 4
2. Assignment Operators
Assignment operators assign values to variables.
- Assignment (=)
let x = 5;
- Addition assignment (+=)
let x = 5; x += 2; // x = 7
- Subtraction assignment (-=)
let x = 5; x -= 2; // x = 3
- Multiplication assignment (*=)
let x = 5; x *= 2; // x = 10
- Division assignment (/=)
let x = 5; x /= 2; // x = 2.5
- Modulus assignment (%=)
let x = 5; x %= 2; // x = 1
- Exponentiation assignment (**=)
let x = 5; x **= 2; // x = 25
3. Comparison Operators
Comparison operators compare two values and return a boolean result (true or false).
- Equal (==)
let x = (5 == 5); // true
- Not equal (!=)
let x = (5 != 2); // true
- Strict equal (===)
let x = (5 === 5); // true let y = (5 === ‘5’); // false
- Strict not equal (!==)
let x = (5 !== ‘5’); // true
- Greater than (>)
let x = (5 > 2); // true
- Greater than or equal (>=)
let x = (5 >= 5); // true
- Less than (<)
let x = (5 < 10); // true
- Less than or equal (<=)
let x = (5 <= 5); // true
4. Logical Operators
Logical operators are used to combine multiple boolean expressions or values.
- Logical AND (&&)
let x = (true && false); // false
- Logical OR (||)
let x = (true || false); // true
- Logical NOT (!)
let x = !true; // false
5. Bitwise Operators
Bitwise operators treat their operands as a set of 32 bits and operate on them bit by bit.
- AND (&)
let x = 5 & 1; // 1 (0101 & 0001)
- OR (|)
let x = 5 | 1; // 5 (0101 | 0001)
- NOT (~)
let x = ~5; // -6 (~0101)
- XOR (^)
let x = 5 ^ 1; // 4 (0101 ^ 0001)
- Left shift (<<)
let x = 5 << 1; // 10 (0101 << 1)
- Right shift (>>)
let x = 5 >> 1; // 2 (0101 >> 1)
- Zero fill right shift (>>>)
let x = 5 >>> 1; // 2 (0101 >>> 1)
6. String Operators
In addition to comparing strings, the + operator can be used to concatenate strings.
- Concatenation (+)
let x = “Hello” + ” ” + “World”; // “Hello World”
- Concatenation assignment (+=)
let x = “Hello”; x += ” World”; // “Hello World”
7. Conditional (Ternary) Operator
The conditional operator assigns a value to a variable based on a condition.
let age = 18; let canVote = (age >= 18) ? “Yes” : “No”; // “Yes”
8. Unary Operators
Unary operators operate on a single operand.
- Unary plus (+)
let x = +”5″; // 5 (converts string to number)
- Unary minus (-)
let x = -5; // -5
- Increment (++)
let x = 5; x++; // 6
- Decrement (–)
let x = 5; x–; // 4
- Logical NOT (!)
let x = !false; // true
- Typeof (typeof)
let x = typeof “Hello”; // “string”
- Delete (delete)
let obj = {name: “Alice”}; delete obj.name; // true
- Void (void)
void function() { console.log(“This will not return anything”); }();
9. Comma Operator
The comma operator allows multiple expressions to be evaluated in a single statement, and returns the value of the last expression.
let x = (1 + 2, 3 + 4); // 7
Example: Combining Various Operators
let a = 10;
let b = 20;
// Arithmetic operators
let sum = a + b; // 30
let diff = a – b; // -10
// Comparison operators
let isEqual = (a == b); // false
let isGreater = (a > b); // false
// Logical operators
let and = (a > 5 && b > 15); // true
let or = (a > 15 || b > 15); // true
// Ternary operator
let max = (a > b) ? a : b; // 20
// String operator
let greeting = “Hello” + ” ” + “World”; // “Hello World”
console.log(sum, diff, isEqual, isGreater, and, or, max, greeting);
This example demonstrates the use of various operators in JavaScript, showcasing how they can be combined to perform different operations and produce desired results.
