JavaScript Operators
Learn about different types of operators in JavaScript and how to use them.
Introduction to Operators
Operators are used to perform operations on variables and values.
Types of Operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- String Operators
- Unary Operators
- Ternary Operator
Arithmetic Operators
+: Addition-: Subtraction*: Multiplication/: Division%: Modulus (Remainder)**: Exponentiation++: Increment--: Decrement
let a = 10, b = 3;
console.log(a + b); // 13
console.log(a % b); // 1
console.log(a ** b); // 1000
Assignment Operators
Used to assign values to variables:
=: Assign+=: Add and assign-=: Subtract and assign*=,/=,%=: Multiply/Divide/Modulus and assign
let x = 5;
x += 3; // x = x + 3 → 8
Comparison Operators
==: Equal (type conversion)===: Strict equal (no conversion)!=,!==: Not equal>,<,>=,<=
console.log(5 == '5'); // true
console.log(5 === '5'); // false
Logical Operators
&&: Logical AND||: Logical OR!: Logical NOT
let age = 20;
console.log(age > 18 && age < 30); // true
String Operator
The + operator can also be used to concatenate strings:
let firstName = "John";
let lastName = "Doe";
console.log(firstName + " " + lastName); // John Doe
Ternary Operator
Short-hand for if-else:
let age = 18;
let result = (age >= 18) ? "Adult" : "Minor";
console.log(result); // Adult
Examples:
// Arithmetic let x = 5 + 3; // 8 // Assignment let y = 10; y += 5; // 15 // Comparison let isEqual = (x == y); // false // Logical let result = (x > 3 && y < 20); // true // String let greeting = "Hello" + " " + "World"; // "Hello World" // Conditional let age = 18; let canVote = (age >= 18) ? "Yes" : "No";