๐ฅ๏ธ 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.
Operator | Purpose | JavaScript Example | Python Example | Equivalent To |
---|---|---|---|---|
= |
Assign value | x = 5; |
x = 5 |
x = 5 |
+= |
Add & Assign | x += 3; |
x += 3 |
x = x + 3 |
-= |
Subtract & Assign | x -= 3; |
x -= 3 |
x = x - 3 |
*= |
Multiply & Assign | x *= 3; |
x *= 3 |
x = x * 3 |
/= |
Divide & Assign | x /= 3; |
x /= 3 |
x = x / 3 |
%= |
Modulus & Assign | x %= 3; |
x %= 3 |
x = x % 3 |
**= |
Exponentiation & Assign | x **= 3; |
x **= 3 |
x = x ** 3 |
๐ Note: In Python, //=
is used for floor division (rounds down the quotient).
๐งฎ 2๏ธโฃ Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers.
Operator | Operation | Example (2 & 3) | Result |
---|---|---|---|
+ |
Addition | 2 + 3 |
5 |
- |
Subtraction | 2 - 3 |
-1 |
* |
Multiplication | 2 * 3 |
6 |
/ |
Division | 2 / 3 |
0.666... |
** |
Exponentiation | 2 ** 3 |
8 |
% |
Modulus (Remainder) | 10 % 3 |
1 |
// |
Floor Division (Python Only) | 10 // 3 |
3 |
๐ 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
.
Operator | Description | JavaScript Example | Python Example | Result (1 & 1) |
---|---|---|---|---|
== |
Equal to | 1 == 1 |
1 == 1 |
true |
=== |
Strict Equal to (Checks Type) | 1 === "1" |
N/A | false |
!= |
Not Equal to | 1 != 2 |
1 != 2 |
true |
!== |
Strict Not Equal to | 1 !== "1" |
N/A | true |
> |
Greater than | 2 > 1 |
2 > 1 |
true |
< |
Less than | 1 < 2 |
1 < 2 |
true |
>= |
Greater than or equal to | 2 >= 2 |
2 >= 2 |
true |
<= |
Less than or equal to | 1 <= 2 |
1 <= 2 |
true |
๐ค 4๏ธโฃ Logical Operators
Logical operators evaluate multiple conditions and return a final true
or false
.
Operator | Description | JavaScript Example | Python Example | Result |
---|---|---|---|---|
&& (and ) |
Returns true if both conditions are true |
true && false |
True and False |
false |
` | ( or`) |
Returns true if at least one condition is true |
`true | |
! (not ) |
Returns the opposite value | !true |
not True |
false |
Example:!(true && false)
โ !(false)
โ true
โก 5๏ธโฃ Bitwise Operators
Bitwise operators manipulate values at the binary level (0s & 1s).
Operator | Operation | Example (5 & 3) | Binary Result |
---|---|---|---|
& |
AND | 5 & 3 |
1 |
` | ` | OR | `5 |
^ |
XOR | 5 ^ 3 |
6 |
~ |
NOT | ~5 |
-6 |
<< |
Left Shift | 5 << 1 |
10 |
>> |
Right Shift | 5 >> 1 |
2 |
๐ 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?
- MDN Web Docs โ JavaScript Operators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference
- W3Schools โ Python Operators: https://www.w3schools.com/python/python_operators.asp