Home/Concepts/Conditionals
🔀
Beginner⏱ 8 min#07 / 08

Conditionals

if/else & Decision Logic

Conditionals let your code make decisions. Based on whether a condition is truthy or falsy, different code paths execute - this is how programs respond dynamically to different inputs.

Your Progress0/8 complete
0 visited0%
💡 Real-World Analogy

Conditionals are traffic lights for your code. Green (if) - proceed. Yellow (else if) - slow down and reconsider. Red (else) - take a different road entirely. Your program always picks exactly one road.

🔑 Key Concepts

if / else if / else

Foundation of decision-making. Evaluates conditions in sequence; executes first truthy block.

Ternary Operator

One-line shorthand: condition ? valueIfTrue : valueIfFalse. Great for simple assignments.

switch & Logical Ops

switch for multi-value checks. && (AND), || (OR), ! (NOT), ?? (nullish coalescing).

💻 Code Examples

1if / else if / else
conditionals.js
1let temp = 35;
2 
3if (temp > 30) {
4 console.log("Hot outside");
5} else if (temp > 20) {
6 console.log("Nice weather");
7} else if (temp > 10) {
8 console.log("A bit cool");
9} else {
10 console.log("Very cold");
11}
12 
13// Truthy & Falsy
14// Falsy: false, 0, "", null, undefined, NaN
15// Everything else is Truthy
16let name = "";
17if (name) {
18 console.log("Has name");
19} else {
20 console.log("No name given"); // this runs
21}
2Comparison & Logical Operators
conditionals.js
1// Always use === (strict equality)
25 === 5; // true (same value and type)
35 === "5"; // false (different type)
45 == "5"; // true (loose - avoid!)
55 !== 3; // true
6 
7// Logical operators
8let age = 20, hasID = true;
9 
10// AND - BOTH must be true
11if (age >= 18 && hasID) {
12 console.log("Entry allowed");
13}
14 
15// OR - at least ONE must be true
16let isWeekend = true, isHoliday = false;
17if (isWeekend || isHoliday) {
18 console.log("Day off");
19}
20 
21// NOT - flips boolean
22let loggedIn = false;
23if (!loggedIn) console.log("Please log in");
24 
25// Nullish coalescing (??)
26let user = null;
27let display = user ?? "Guest"; // "Guest"
3Ternary & Switch
conditionals.js
1// Ternary - one-liner if/else
2let score = 85;
3let result = score >= 60 ? "Pass" : "Fail";
4 
5// Chained ternary (keep readable)
6let grade = score >= 90 ? "A"
7 : score >= 80 ? "B"
8 : score >= 70 ? "C" : "F";
9 
10// Switch - clean multi-case checks
11let day = "Monday";
12switch (day) {
13 case "Saturday":
14 case "Sunday":
15 console.log("Weekend"); break;
16 case "Monday":
17 case "Tuesday":
18 case "Wednesday":
19 case "Thursday":
20 case "Friday":
21 console.log("Weekday"); break;
22 default:
23 console.log("Invalid day");
24}
25 
26// Short-circuit default value
27let config = {};
28config.theme = config.theme || "dark";

⚠️ Common Mistakes

❌ Mistake

Using == instead of === (type coercion surprises)

✅ Fix

Always use === for comparisons. == does type coercion: 0 == false is true!

❌ Mistake

Forgetting break in switch cases

✅ Fix

Without break, execution falls through to the next case. Always add break (or return).

❌ Mistake

if (x = 5) - accidental assignment

✅ Fix

= is assignment, === is comparison. if (x === 5) is correct.

📋 Quick Reference

if(cond) {} else if(cond) {} else {}cond ? trueVal : falseValswitch(v) { case x: break; }=== strict equality&& AND, || OR, ! NOT?? nullish coalescingFalsy: false 0 "" null undefined NaN

Live Code Playground

Edit the code and press ▶ Run to see output instantly.

conditionals_playground.js
⟳ Loading editor...
💾 Auto-saved
// Press ▶ Run Code to execute and view output console logs...
⚙️
← Previous
Functions
Challenge Mode
Next →
DOM Manipulation
🌐