JavaScript Operators

📘 JavaScript Operators

In JavaScript, operators are special symbols used to perform operations on values and variables.

For example:

let x = 5 + 3; // Here "+" is an operator

1. Arithmetic Operators

Used to perform basic mathematical operations.

Operator                   Name                                            Example                                  Result              
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division10 / 25
%Modulus (remainder)5 % 21
**Exponentiation2 ** 38
++Incrementlet x=5; x++ → 6Increases by 1
--Decrementlet x=5; x-- → 4Decreases by 1

2. Assignment Operators

Used to assign values to variables.

Operator                Example                   Same as    
=x = 10x = 10
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
**=x **= 2x = x ** 2

3. Comparison Operators

Used to compare two values. They return true or false.

Operator                 Meaning                                        Example                 Result
==Equal to (value only)5 == "5"true
===Equal value & type5 === "5"false
!=Not equal (value only)5 != "5"false
!==Not equal (value + type)5 !== "5"true
>Greater than10 > 5true
<Less than10 < 5false
>=Greater than or equal10 >= 10true
<=Less than or equal5 <= 10true

4. Logical Operators

Used to combine conditions.

Operator              Meaning              Example                          Result
&&AND(5 > 3 && 10 > 5)true
||OR(5 > 3   ||    10 > 11)true
!NOT!(5 > 3)false

5. Bitwise Operators

Operate on binary numbers (rarely used for beginners, but important).

Operator           Name                   Example       
&AND5 & 1 → 1
|OR5 | 1 → 5
^XOR5 ^ 1 → 4
~NOT~5 → -6
<<Left shift5 << 1 → 10
>>Right shift5 >> 1 → 2

6. String Operators

The + operator can also concatenate strings.

let firstName = "Adib"; let lastName = "Mahfuj"; console.log(firstName + " " + lastName); // Output: "Adib Mahfuj"

📌 += can also be used with strings:

let text = "Hello"; text += " World"; console.log(text); // "Hello World"

7. Type Operators

Operator                   Usage
typeofReturns the type of a variable → typeof 123 → "number"
instanceofChecks if an object is an instance of a class → arr instanceof Array → true

8. Ternary Operator (?:)

Shortcut for an if-else statement.

let age = 18; let result = (age >= 18) ? "Adult" : "Minor"; console.log(result); // Adult

🔑 Summary

  • Arithmetic → Math operations

  • Assignment → Assign values

  • Comparison → Compare values (true/false)

  • Logical → Combine conditions

  • Bitwise → Binary operations

  • String → Concatenation

  • Type → Type checking

  • Ternary → Short if-else

Comments