JavaScript Operators

Operators in JavaScript are symbols used to perform operations on variables and values. They are essential for writing logic, calculations, and conditions

1. Arithmetic Operators

Used for mathematical calculations.

OperatorDescriptionExample
+Addition5 + 2 = 7
-Subtraction5 - 2 = 3
*Multiplication5 * 2 = 10
/Division10 / 2 = 5
%Modulus (remainder)5 % 2 = 1
**Exponentiation2 ** 3 = 8

Example:

let a = 10;
let b = 3;console.log(a + b); // 13
console.log(a % b); // 1

2. Assignment Operators

Used to assign values to variables.

OperatorExampleMeaning
=x = 5Assign 5
+=x += 3x = x + 3
-=x -= 2x = x – 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2

Example:

let x = 5;
x += 3; // 8

3. Comparison Operators

Used to compare two values (returns true/false).

OperatorDescription
==Equal (value only)
===Strict equal (value + type)
!=Not equal
!==Strict not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

Example:

console.log(5 == "5");   // true
console.log(5 === "5"); // false

4. Logical Operators

Used to combine conditions.

OperatorDescription
&&AND
`
!NOT

Example:

let age = 20;if (age > 18 && age < 30) {
console.log("Young adult");
}

5. Unary Operators

Operate on a single operand.

OperatorDescription
++Increment
--Decrement
typeofReturns type
!Logical NOT

Example:

let num = 5;
num++; // 6console.log(typeof num); // number

6. Ternary Operator

Short form of if-else.

Syntax:

condition ? value1 : value2;

Example:

let age = 18;
let result = age >= 18 ? "Adult" : "Minor";

7. Special Operators

Some useful additional operators:

OperatorDescription
?.Optional chaining
??Nullish coalescing
inCheck the property in the object
instanceofCheck object type

Example:

let user = {};
console.log(user?.name); // undefined (no error)let value = null ?? "default";
console.log(value); // "default"

✅ Summary

  • Arithmetic → calculations
  • Assignment → assign values
  • Comparison → compare values
  • Logical → combine conditions
  • Ternary → short if-else
  • Special → modern JS features

Leave a Comment