𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗖𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗦𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁𝘀
Your code needs to make decisions. Conditional statements control your program flow. They run specific code based on a condition.
Use these three types:
- if: Runs code when a condition is true.
- if-else: Runs one block if true and another if false.
- else if: Checks multiple conditions in order.
Learn truthy and falsy values. JavaScript treats non-boolean values as true or false.
Truthy values:
- Non-zero numbers.
- Non-empty strings.
- Objects and arrays.
Falsy values:
- false.
- Empty strings.
- null.
- undefined.
- NaN.
Watch for these interview traps:
- The string "0" is truthy. It is not empty.
- A single equals sign (=) assigns a value. Use triple equals (===) to compare values.