1. Definition

An operator in JavaScript is a symbol used to perform operations on operands (variables and values). Operators are essential for performing arithmetic, comparing values, assigning values, logical evaluation, and more.


2. Types of JavaScript Operators

CategoryExamples
Arithmetic Operators+, -, *, /, %, **
Assignment Operators=, +=, -=, *=, /=, etc.
Comparison Operators==, ===, !=, !==, <, >
Logical Operators&&, `
Unary Operatorstypeof, ++, --, !, delete
Ternary Operatorcondition ? expr1 : expr2
Bitwise Operators&, `

3. Arithmetic Operators

Used to perform mathematical calculations.

OperatorDescriptionExampleOutput
+Addition2 + 35
-Subtraction5 - 23
*Multiplication4 * 28
/Division10 / 25
%Modulus (remainder)7 % 21
**Exponentiation (ES6)2 ** 38

4. Assignment Operators

Used to assign values to variables.

OperatorDescriptionExample
=Assignx = 10
+=Add and assignx += 5x = x + 5
-=Subtract and assignx -= 3
*=Multiply and assignx *= 2
/=Divide and assignx /= 4
%=Modulus and assignx %= 2

5. Comparison Operators

Used to compare values. Results in a boolean (true or false).

OperatorDescriptionExampleOutput
==Equal (type conversion allowed)5 == '5'true
===Strict equal (no type conversion)5 === '5'false
!=Not equal5 != '5'false
!==Strict not equal5 !== '5'true
>Greater than6 > 3true
<Less than3 < 6true
>=Greater than or equal5 >= 5true
<=Less than or equal5 <= 6true

6. Logical Operators

Used to combine multiple boolean expressions.

OperatorDescriptionExampleOutput
&&Logical ANDtrue && falsefalse
``Logical OR
!Logical NOT!truefalse

7. Unary Operators

Work with only one operand.

a. typeof

  • Returns the data type of a variable.

typeof "hello"; // "string"

b. ++ (Increment) / — (Decrement)

Increases or decreases a variable’s value by 1.

javascript

CopyEdit

let x = 5; x++; // 6

c. delete

Deletes a property from an object.

javascript

CopyEdit

let obj = {name: "John"}; delete obj.name; // obj becomes {}


8. Ternary Operator (Conditional Operator)

Short form of if...else. Used for decision-making.

Syntax:

condition ? expressionIfTrue : expressionIfFalse

Example:

let age = 20; let status = age >= 18 ? "Adult" : "Minor"; // "Adult"

9. Bitwise Operators

Operate on 32-bit binary numbers.

OperatorDescriptionExampleOutput
&AND5 & 11
``OR`5
^XOR5 ^ 14
~NOT~5-6
<<Left shift5 << 110
>>Right shift5 >> 12

10. String Operators

Used to concatenate strings.

a. + (Concatenation)

"Hello" + " World"; // "Hello World"

b. +=

let str = "Hello"; str += " World"; // "Hello World"

11. Spread and Rest Operators (...)

a. Spread

Expands elements of an array or object.

let nums = [1, 2, 3]; let copy = [...nums]; // [1, 2, 3]

b. Rest

Gathers remaining arguments into an array.

function sum(...args) {   return args.reduce((a, b) => a + b, 0); } sum(1, 2, 3); // 6`