Advertisement

Google Ad Slot: content-top

JavaScript Operators


JavaScript operators are symbols or keywords used to perform operations on variables and values. They are fundamental building blocks for writing logic in JavaScript.


operators:


Arithmetic Operators:

Used to perform basic mathematical operations.

Operator

Description

Example

Result

+

Addition

5 + 2

7

-

Subtraction

5 - 2

3

*

Multiplication

5 * 2

10

/

Division

5 / 2

2.5

%

Modulus (Remainder)

5 % 2

1

**

Exponentiation

5 ** 2

25

++

Increment

let a = 1; a++

2

--

Decrement

let a = 1; a--

0

Example
let a = 5;
let b = 2;
console.log("Addition: "+a + b);
console.log("Subtraction: "+(a - b));
console.log("Multiplication: "+a * b);
console.log("Division: "+a / b);
console.log("Remainder: "+a % b);
console.log("Exponentiation: "+a ** b);
console.log("Increment: "+a++);
console.log("Decrement: "+a--);
Try it yourself

Assignment Operators:

Used to assign values to variables.

Operator

Description

Example

Equivalent To

=

Assign

a = 5

-----

+=

Add and Assign

a += 2

a = a + 2

-=

Subtract and Assign

a -= 2

a = a - 2

*=

Multiply and Assign

a *= 2

a = a * 2

/=

Divide and Assign

a /= 2

a = a / 2

%=

Modulus and Assign

a %= 2

a = a % 2

**=

Exponentiate Assign

a **= 2

a = a ** 2

Example
let a = 5;
console.log("Assign: "+a);
a += 2;
console.log("Add and Assign: "+a);
a -= 2;
console.log("Subtract and Assign: "+a);
a *= 2;
console.log("Multiply and Assign: "+a);
a /= 2;
console.log("Divide and Assign: "+a);
a %= 2;
console.log("Modulus and Assign: "+a);
a **= 2;
console.log("Exponentiate Assign: "+a);
Try it yourself

Comparison Operators:

Used to compare two values.

Operator

Description

Example

Result

==

Equal to

5 == "5"

true

===

Strict Equal (type & value)

5 === "5"

false

!=

Not Equal

5 != "5"

false

!==

Strict Not Equal

5 !== "5"

true

>

Greater Than

5 > 2

true

<

Less Than

5 < 2

false

>=

Greater or Equal

5 >= 5

true

<=

Less or Equal

5 <= 5

true

Example
console.log("Equal to: "+(5 == "5"));
console.log("Strict Equal (type & value): "+(5 === "5"));
console.log("Not Equal: "+(5 != "5"));
console.log("Strict Not Equal: "+(5 !== "5"));
console.log("Greater Than: "+(5 > 2));
console.log("Less Than: "+(5 < 2));
console.log("Greater or Equal: "+(5 >= 5));
console.log("Less or Equal: "+(5 <= 5));
Try it yourself

Logical Operators:

Used to combine or invert Boolean values.

Operator

Description

Example

Result

&&

AND

true && false

false

||

OR

true || false

true

!

NOT

!true

false

Example
// AND operator
console.log("AND: "+ (true && false));
console.log("AND: "+ (5 && 0));
console.log("AND: "+ (0 && 5));
console.log("AND: "+ (5 && 1));
console.log("AND: "+ (1 && 5));
console.log("\n");

// OR operator
console.log("OR: "+(true || false));
console.log("OR: "+(5 || 0));
console.log("OR: "+(0 || 5));
console.log("AND: "+ (5 || 1));
console.log("AND: "+ (1 || 5));
console.log("\n");

// NOT operator
console.log("NOT: "+(!true));
console.log("NOT: "+(!5));
console.log("NOT: "+(!0));
Try it yourself

Bitwise Operators:

Operate on binary numbers.

Operator

Description

Example

Result

&

AND

5 & 1

1

|

OR

5 | 1

5

^

XOR

5 ^ 1

4

~

NOT

~5

-6

<<

Left Shift

5 << 1

10

>>

Right Shift

5 >> 1

2

Example
// AND operator
console.log("AND: "+ (5 & 1));
console.log("\n");

// OR operator
console.log("OR: "+(5 | 1));
console.log("\n");

// XOR operator
console.log("XOR: "+(5 ^ 1));
console.log("\n");

// NOT operator
console.log("NOT: "+(~5));
console.log("\n");

// Left Shift operator
console.log("Left Shift: "+(5 << 1));
console.log("\n");

// Right Shift operator
console.log("Right Shift: "+(5 >> 1));
console.log("\n");
Try it yourself

Type Operators:

Determine or manipulate variable types.

Operator

Description

Example

Result

typeof

Type of variable

typeof "hello"

"string"

instanceof

Check instance of object

[] instanceof Array

true

Example
console.log(typeof "hello");
console.log([] instanceof Array);
Try it yourself

Ternary Operator:

A shorthand for if-else.

Example
let age = 20;
let status = age >= 18 ? "Adult" : "Minor";
console.log(status); // Adult
Try it yourself

Nullish Coalescing Operator (??)

Returns the right-hand value if the left-hand value is null or undefined.

Example
let value = null ?? 'default';
console.log(value); // default
Try it yourself

Optional Chaining (?.)

Access nested object properties safely.

Example
let user = { name: 'Alice' };
console.log(user.address?.city); // undefined (no error)
Try it yourself