What are Operators in JavaScript?
- Operators are symbols used to perform operations on values and variables.
Types of Operators in JavaScript
1. Arithmetic Operators:
Used to perform mathematical calculations
Operators List:
+ → Addition
- → Subtraction
* → Multiplication
/ → Division
% → Modulus
let a = 10;
let b = 5;
console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2
console.log(a % b); // 0
2. Assignment Operators:
Used to assign values to variables
let x = 10;
x += 5; // x = x + 5 → 15
x -= 2; // x = x - 2 → 13
=
+=
-=
*=
/=
3. Comparison Operators:
Used to compare two values (returns true/false)
console.log(10 > 5); // true
console.log(10 == "10"); // true
console.log(10 === "10"); // false
== → Equal (value only)
=== → Strict Equal (value + type)
!= → Not equal
> < >= <=
4. Logical Operators:
Used to combine conditions
let age = 25;
console.log(age > 18 && age < 30); // true
console.log(age > 18 || age < 20); // true
console.log(!(age > 18)); // false
&& → AND
|| → OR
! → NOT
5. Unary Operators:
Operate on one value
let a = 5;
a++; // increment → 6
a--; // decrement → 5
- Type Operators (Need to be discussed)
Used to check data types
let name = "Anees";
console.log(typeof name); // string
Type Coercion:
JavaScript sometimes automatically converts types
console.log(10 - "2"); // 8
console.log(10 + "2"); // "102"
Top comments (0)