Both ++counter and counter++ increment IMMEDIATELY
The difference is what they RETURN, not when they increment:
Prefix (++counter):
let counter = 1;
let a = ++counter;
// Step 1: counter becomes 2 (increments first)
// Step 2: returns the NEW value (2)
// So a = 2Postfix (counter++):
let counter = 1;
let a = counter++;
// Step 1: remembers OLD value (1)
// Step 2: counter becomes 2 (increments)
// Step 3: returns the OLD value (1)
// So a = 1Key point:
In both cases, counter ends up being 2! The increment happens immediately in both cases.
javascript
// Prefix
let counter = 1;
let a = ++counter;
alert(counter); // 2 (incremented)
alert(a); // 2 (returned new value)
// Postfix
let counter = 1;
let a = counter++;
alert(counter); // 2 (incremented!)
alert(a); // 1 (returned old value)Rule 1: The + Operator is Special
- If ANY operand is a string → String concatenation (left to right)
- If NO strings → Numeric addition
Rule 2: All Other Math Operators (-, *, /, %)
- Always convert to numbers first
Rule 3: Conversion to Numbers
javascript
// These become numbers:
"123" → 123
" 123 " → 123 (trims whitespace)
"" → 0 (empty string = 0)
" " → 0 (whitespace only = 0)
true → 1
false → 0
null → 0
undefined → NaN
"123abc" → NaN (invalid number)Let’s Fix Your Answers:
javascript
"" + 1 + 0 // "10" (string + number = string concatenation)
"" - 1 + 0 // -1 ✓ (- converts to numbers: 0 - 1 + 0)
true + false // 1 ✓ (no strings, so: 1 + 0 = 1)
6 / "3" // 2 ✓ (division converts: 6 / 3)
"2" * "3" // 6 ✓ (multiplication converts: 2 * 3)
4 + 5 + "px" // "9px" ✓ (4 + 5 = 9, then 9 + "px" = "9px")
"$" + 4 + 5 // "$45" (string + 4 = "$4", then "$4" + 5 = "$45")
"4" - 2 // 2 ✓ (subtraction converts: 4 - 2)
"4px" - 2 // NaN ✓ ("4px" can't convert to number)
" -9 " + 5 // " -9 5" (string concatenation)
" -9 " - 5 // -14 ✓ (subtraction converts: -9 - 5)
null + 1 // 1 (null converts to 0, so 0 + 1 = 1)
undefined + 1 // NaN ✓ (undefined converts to NaN)
" \t \n" - 2 // -2 (whitespace converts to 0, so 0 - 2 = -2)Quick Decision Tree:
- Is there a
+and a string? → String concatenation - Is it any other math operator? → Convert everything to numbers
- Left to right evaluation for same precedence
Bottom line: + with strings = concatenation, everything else = numeric conversion!
Logical Operators
What they do: Work with any values (not just booleans), return original values (not just true/false)
Key Behavior - They Don’t Just Return true/false:
1. || (OR) - Finds first TRUTHY value
javascript
1 || 0 // 1 (returns first truthy)
null || "hello" || 0 // "hello" (first truthy)
false || false || 5 // 5 (first truthy)
null || undefined || 0 // 0 (all falsy, returns last)2. && (AND) - Finds first FALSY value
javascript
1 && 0 // 0 (returns first falsy)
"hello" && 5 && null // null (first falsy)
1 && 2 && 3 // 3 (all truthy, returns last)
0 && "won't run" // 0 (first falsy, stops immediately)3. ! (NOT) - Converts to boolean and flips
javascript
!true // false
!0 // true (0 is falsy)
!"hello" // false ("hello" is truthy)
!!5 // true (double NOT = convert to boolean)Short-Circuit Evaluation:
|| stops at first truthy, && stops at first falsy
javascript
true || alert("won't run") // doesn't alert (stops at true)
false && alert("won't run") // doesn't alert (stops at false)Common Patterns:
javascript
// Default values with ||
const name = userName || "Guest"; // if userName is falsy, use "Guest"
// Conditional execution with &&
user.isAdmin && showAdminPanel(); // only runs if user.isAdmin is truthy
// Convert to boolean with !!
const isValid = !!someValue; // converts any value to true/falseInterview gotchas:
javascript
// These trip people up
0 || 5 // 5 (not true!)
"" && "hello" // "" (not false!)
null || undefined || 0 // 0 (returns last falsy)
1 && 2 && 3 // 3 (returns last truthy)
// Precedence: ! > && > ||
!false && true || false // true (evaluated as: ((!false) && true) || false)Bottom line: || returns first truthy or last value, && returns first falsy or last value. They return actual values, not just booleans!
