JavaScript Operators

JavaScript Operators

JavaScript Operators Explained

JavaScript operators are symbols that perform operations on variables and values. They can be categorized into different types:

1. Arithmetic Operators 🧮

Used for basic mathematical operations.

OperatorDescriptionExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 212
/Division10 / 25
%Modulus (Remainder)10 % 31
**Exponentiation2 ** 38
++Incrementlet x = 5; x++6
--Decrementlet y = 8; y--7

2. Assignment Operators 📝

Used to assign values to variables.

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

3. Comparison Operators 🔍

Used to compare two values and return true or false.

OperatorDescriptionExampleResult
==Equal to5 == '5'true
===Strict equal (type & value)5 === '5'false
!=Not equal10 != 5true
!==Strict not equal10 !== '10'true
>Greater than10 > 5true
<Less than4 < 7true
>=Greater than or equal to10 >= 10true
<=Less than or equal to6 <= 3false

4. Logical Operators 🤔

Used to combine multiple conditions.

OperatorDescriptionExampleResult
&&AND (both must be true)true && falsefalse
``OR (at least one must be true)
!NOT (reverses value)!truefalse

5. Bitwise Operators 🖥️

Perform bitwise operations on numbers.

OperatorDescriptionExample (5 & 1)Result
&AND5 & 11
``OR`5
^XOR5 ^ 14
~NOT~5-6
<<Left Shift5 << 110
>>Right Shift5 >> 12

6. Ternary Operator (Conditional Operator) 🎭

Shorter way to write an if-else statement.

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

7. Type Operators 🏷️

Used to check the type of a variable.

OperatorDescriptionExampleResult
typeofReturns variable typetypeof "Hello""string"
instanceofChecks if an object is an instance of a classobj instanceof Objecttrue

8. Spread and Rest Operators (...) 📌

Spread (...) – Expands elements

let numbers = [1, 2, 3]; let newNumbers = [...numbers, 4, 5]; console.log(newNumbers); // [1, 2, 3, 4, 5]

Rest (...) – Collects remaining values into an array

function sum(...values) { return values.reduce((a, b) => a + b, 0); } console.log(sum(1, 2, 3, 4)); // 10

Conclusion

JavaScript operators are fundamental for performing operations on values.

  • Arithmetic (+, -, *, /, %, ++, --)
  • Assignment (=, +=, -=, *=, /=, %=)
  • Comparison (==, ===, !=, !==, >, <, >=, <=)
  • Logical (&&, ||, !)
  • Ternary (condition ? trueValue : falseValue)
  • Spread & Rest (...)

Want an example of a specific operator in action?

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close