๐Ÿ–ฅ๏ธ Understanding Operators in Programming ๐Ÿš€

๐Ÿ“Œ Introduction

Operators are essential in programming. They allow us to perform mathematical calculations, assign values, compare data, and make logical decisions. Understanding how different types of operators work is crucial for writing efficient and bug-free code.

In this post, weโ€™ll explore:
โœ… Assignment Operators โ€“ Assigning values to variables
โœ… Arithmetic Operators โ€“ Performing calculations
โœ… Comparison Operators โ€“ Comparing values
โœ… Logical Operators โ€“ Making decisions in code
โœ… Bitwise Operators โ€“ Working with binary values
โœ… Truthy & Falsy Values โ€“ How different values behave in conditions
โœ… Equality vs. Identity โ€“ Checking if values are truly the same


๐Ÿ”ข 1๏ธโƒฃ Assignment Operators

Assignment operators allow you to store and modify values in variables.

OperatorPurposeJavaScript ExamplePython ExampleEquivalent To
=Assign valuex = 5;x = 5x = 5
+=Add & Assignx += 3;x += 3x = x + 3
-=Subtract & Assignx -= 3;x -= 3x = x - 3
*=Multiply & Assignx *= 3;x *= 3x = x * 3
/=Divide & Assignx /= 3;x /= 3x = x / 3
%=Modulus & Assignx %= 3;x %= 3x = x % 3
**=Exponentiation & Assignx **= 3;x **= 3x = x ** 3

๐Ÿ“Œ Note: In Python, //= is used for floor division (rounds down the quotient).


๐Ÿงฎ 2๏ธโƒฃ Arithmetic Operators

Arithmetic operators perform mathematical calculations on numbers.

OperatorOperationExample (2 & 3)Result
+Addition2 + 35
-Subtraction2 - 3-1
*Multiplication2 * 36
/Division2 / 30.666...
**Exponentiation2 ** 38
%Modulus (Remainder)10 % 31
//Floor Division (Python Only)10 // 33

๐Ÿ“Œ Order of Operations (PEMDAS): Parentheses, Exponents, Multiplication/Division, Addition/Subtraction.

Example:
2 + 3 * 4 โ†’ 2 + 12 โ†’ 14
(2 + 3) * 4 โ†’ 5 * 4 โ†’ 20


๐Ÿ” 3๏ธโƒฃ Comparison Operators

Comparison operators compare two values and return true or false.

OperatorDescriptionJavaScript ExamplePython ExampleResult (1 & 1)
==Equal to1 == 11 == 1true
===Strict Equal to (Checks Type)1 === "1"N/Afalse
!=Not Equal to1 != 21 != 2true
!==Strict Not Equal to1 !== "1"N/Atrue
>Greater than2 > 12 > 1true
<Less than1 < 21 < 2true
>=Greater than or equal to2 >= 22 >= 2true
<=Less than or equal to1 <= 21 <= 2true

๐Ÿค” 4๏ธโƒฃ Logical Operators

Logical operators evaluate multiple conditions and return a final true or false.

OperatorDescriptionJavaScript ExamplePython ExampleResult
&& (and)Returns true if both conditions are truetrue && falseTrue and Falsefalse
`(or`)Returns true if at least one condition is true`true
! (not)Returns the opposite value!truenot Truefalse

Example:
!(true && false) โ†’ !(false) โ†’ true


โšก 5๏ธโƒฃ Bitwise Operators

Bitwise operators manipulate values at the binary level (0s & 1s).

OperatorOperationExample (5 & 3)Binary Result
&AND5 & 31
``OR`5
^XOR5 ^ 36
~NOT~5-6
<<Left Shift5 << 110
>>Right Shift5 >> 12

๐Ÿ“Œ Why Use Bitwise Operators? They optimize low-level computations (e.g., performance-critical applications).


โœ… 6๏ธโƒฃ Truthy & Falsy Values

Not all values need to be Boolean (true or false) to be used in conditions. Some values are treated as “truthy” or “falsy”.

๐Ÿš€ Falsy Values in JavaScript:

  • false
  • 0, -0, NaN
  • "" (empty string)
  • null, undefined

๐Ÿš€ Falsy Values in Python:

  • False
  • 0, 0.0
  • "", [], {} (Empty collections)
  • None

Everything else is truthy!

Example:

if (0) { console.log("Truthy!"); } else { console.log("Falsy!"); }

Output: Falsy!


๐Ÿ”„ 7๏ธโƒฃ Equality vs. Identity

Equal (==): Checks if values are the same.
Strict Equal (===): Checks if values & types match.
Identity (is in Python): Checks if two variables reference the same object.

JavaScript:

console.log(1 == "1"); // true (values match)
console
.log(1 === "1"); // false (type mismatch)

Python:
x = [1, 2, 3]
y = x
print(x is y) # True (Same object)
print(x == y) # True (Same values)

๐Ÿ“Œ Use strict equality (===) in JavaScript to avoid unexpected type coercion.


๐ŸŽฏ Key Takeaways

โœ… Operators perform calculations, assignments, comparisons, and logical decisions
โœ… PEMDAS/BODMAS rules apply for arithmetic operations
โœ… Logical operators (&&, ||, !) help control program flow
โœ… Truthy & Falsy values affect conditional statements
โœ… Use === (JS) and is (Python) for strict identity checks

Mastering operators is essential for writing efficient and logical programs! ๐Ÿš€


๐Ÿ”— Want to Learn More?