Challenge 1 +20 XP
Write an if statement that logs 'Adult' if age is 18 or higher.
- Test 1 — expects "Adult"
Need a hint? (−25% XP)
Use age >= 18 inside the if condition.
Show solution (0 XP)
const age = 20;
if (age >= 18) {
console.log('Adult');
} The if statement executes a block of code if a specified condition evaluates to truthy. It forms the core foundational structure of conditional logic in JavaScript, letting a program choose between paths at run time and make decisions and reliably branch their internal behavior based on variables and incoming data.
if (10 > 5) {
console.log('Math works');
}Math works
Because if statements cleverly evaluate truthiness automatically, they natively directly coerce their condition to a boolean format. You absolutely don’t need to write explicit checks like if (name !== "") when if (name) will effectively suffice.
Can you predict the final score? Remember your truthy and falsy rules:
Read the code. What exactly will it print? Commit to an answer before you look.
let score = 0;
if ("") {
score += 10;
}
if (" ") {
score += 5;
}
console.log(score);5
else and else if ClausesIf the initial condition is entirely falsy, you can use the else statement to provide a fallback block of code that will execute instead.
if (5 > 10) {
console.log('Math is broken');
} else {
console.log('Math still works');
}Math still works
To seamlessly chain multiple conditional checks together, you can nest if...else statements to create an else if clause. Note that there is absolutely no dedicated elseif keyword in JavaScript—it is literally just an else block containing a completely new if statement!
const score = 85;
if (score > 90) {
console.log('A');
} else if (score > 80) {
console.log('B');
} else {
console.log('C');
}B
Variables safely declared with let or const inside an if block are completely strictly isolated to that specific block scope. They securely cannot be accessed from the outside global or function scope.
if (true) {
let secret = 'hidden';
}
try {
console.log(secret);
} catch (e) {
console.log('Error: secret is not defined');
}Error: secret is not defined
JavaScript permissively allows you to lazily omit the curly braces {} of an if statement if you only want to execute a single line conditionally.
if (false)
console.log('Hidden');
console.log('Always prints');Always prints
In Python, indentation is the block. In JavaScript it is only whitespace, and the parser ignores it. So the second console.log below, lined up neatly with the first, runs whether the condition was true or not.
Without braces, an if governs exactly one statement: the next one. Everything after that is ordinary code that happens to be indented.
Write the braces even for a single line. They cost two characters, and the bug they prevent arrives later — when someone adds a second line and the indentation quietly lies about what runs.
You must explicitly check if (isActive === true).
False, if statements evaluate truthiness automatically, so if (isActive) is sufficient. — Because if statements evaluate truthiness automatically, you can just write if (isActive). This is cleaner and universally preferred.
Variables created with let in an if block are available outside of it.
False, let and const are strictly block-scoped and disappear when the block ends. — Only if you use the outdated 'var' keyword. Modern variables (let and const) are strictly block-scoped and disappear when the block ends.
There is a distinct 'elseif' keyword in JavaScript.
False, JavaScript just strings together an 'else' block immediately followed by an 'if' block. — Unlike Python (elif) or PHP (elseif), JavaScript just strings together an 'else' block immediately followed by an 'if' block. 'else if' is just two separate keywords.
Write an if statement that logs 'Adult' if age is 18 or higher.
Use age >= 18 inside the if condition.
const age = 20;
if (age >= 18) {
console.log('Adult');
} Write an if/else statement that logs 'Pass' if score is 50 or higher, and 'Fail' otherwise.
Use an else block for the 'Fail' condition.
const score = 45;
if (score >= 50) {
console.log('Pass');
} else {
console.log('Fail');
}