Let’s go through Operators in JavaScript step by step.


πŸ”Ή Operators in JavaScript

πŸ‘‰ Operators are symbols that tell JavaScript to perform some operation on values (operands).

Example:

let x = 5 + 3; // + is an operator, 5 & 3 are operands

1️⃣ Arithmetic Operators

Used for mathematical operations.

OperatorDescriptionExampleResult
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22.5
%Modulus (remainder)5 % 21
**Exponentiation2 ** 38
++Incrementx++x = x + 1
--Decrementx--x = x - 1

2️⃣ Assignment Operators

Used to assign values.

OperatorExampleEquivalent To
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
**=x **= 2x = x ** 2

3️⃣ Comparison Operators

Used to compare values (returns true or false).

OperatorDescriptionExampleResult
==Equal to (value only)5 == "5"true
===Strict equal (value + type)5 === "5"false
!=Not equal (value only)5 != "5"false
!==Strict not equal (value + type)5 !== "5"true
>Greater than7 > 5true
<Less than7 < 5false
>=Greater or equal5 >= 5true
<=Less or equal3 <= 5true

4️⃣ Logical Operators

Used with boolean (true / false).

OperatorDescriptionExampleResult
&&Logical ANDtrue && falsefalse
||Logical OR`true
!Logical NOT!truefalse

5️⃣ Bitwise Operators (works on 32-bit numbers)

OperatorDescriptionExampleResult
&AND5 & 11
|OR5 | 15
^XOR5 ^ 14
~NOT~5-6
<<Left shift5 << 110
>>Right shift5 >> 12

6️⃣ Ternary Operator (Conditional)

Shorthand for if...else.

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

7️⃣ Type Operators

OperatorDescriptionExampleResult
typeofReturns type of variabletypeof 123"number"
instanceofChecks if object is an instance of a classarr instanceof Arraytrue

βœ… Summary

  • Arithmetic β†’ + - * / % ** ++ --
  • Assignment β†’ = += -= *= /= %= **=
  • Comparison β†’ == === != !== > < >= <=
  • Logical β†’ && || !
  • Bitwise β†’ & | ^ ~ << >>
  • Ternary β†’ condition ? trueValue : falseValue
  • Type β†’ typeof, instanceof

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *