JavaScript Curriculum
Making Decisions
easyThe Nexus app needs to greet users differently based on their role. Admins see the control panel. Regular users see their dashboard. Guests see a sign-up prompt. A single greeting won't cut it anymore. You need code that looks at who's there — and responds differently each time.
The problem with one-size-fits-all code
Everything you've written so far runs the same way every time. Same input, same output — no choices, no branching.
Real applications make decisions constantly. Is this user logged in? Do they have permission? Is the value valid? Every one of those questions needs code that can go different directions based on the answer.
That's what if / else is for.
The anatomy of an if statement
Three parts: the keyword if, the condition in parentheses, and the block in curly braces. The block only runs if the condition evaluates to true.
If isLoggedIn were false, nothing would print. The block gets skipped entirely.
Adding a fallback with else
else is your default path. It runs whenever the if condition is false — and it's optional. Not every if needs an else.
Handling multiple cases with else if
When you have more than two possible outcomes, chain else if blocks between your if and else:
Click through the animation below. Pick a role and watch exactly which lines JavaScript evaluates — and which ones it skips:
Conditions are just expressions that produce true or false
Anything that evaluates to a boolean works as a condition. The most common source: comparison operators.
Click any operator — then edit the values to see the result update live:
Same value AND same type
Always use === not == — double equals does type coercion and produces surprising results. Triple equals is strict and predictable.
Live condition evaluation
Drag the slider and watch each condition evaluate in real time. Notice that JavaScript stops checking once it finds the first true:
JavaScript checks each condition top to bottom and stops at the first true one. Only one branch ever runs.
Functions that return different values
The pattern you'll use constantly: a function that inspects its input and returns different output based on what it finds.
Common patterns you'll use every day
Your challenge
Write getGreeting(role) — a function that returns a different string for three different roles. Click the animation above with each role to visualise what your code needs to do.
The pattern is: if → else if → else. Three conditions, one function, three possible outputs.
Challenge
Write a function called getGreeting(role) that returns different strings based on role: if role is \"admin\" return \"Welcome to the control panel.\", if role is \"user\" return \"Welcome back!\", otherwise return \"Please sign up to continue.\". Then log the result for all three roles.
Making Decisions
easyThe Nexus app needs to greet users differently based on their role. Admins see the control panel. Regular users see their dashboard. Guests see a sign-up prompt. A single greeting won't cut it anymore. You need code that looks at who's there — and responds differently each time.
The problem with one-size-fits-all code
Everything you've written so far runs the same way every time. Same input, same output — no choices, no branching.
Real applications make decisions constantly. Is this user logged in? Do they have permission? Is the value valid? Every one of those questions needs code that can go different directions based on the answer.
That's what if / else is for.
The anatomy of an if statement
Three parts: the keyword if, the condition in parentheses, and the block in curly braces. The block only runs if the condition evaluates to true.
If isLoggedIn were false, nothing would print. The block gets skipped entirely.
Adding a fallback with else
else is your default path. It runs whenever the if condition is false — and it's optional. Not every if needs an else.
Handling multiple cases with else if
When you have more than two possible outcomes, chain else if blocks between your if and else:
Click through the animation below. Pick a role and watch exactly which lines JavaScript evaluates — and which ones it skips:
Conditions are just expressions that produce true or false
Anything that evaluates to a boolean works as a condition. The most common source: comparison operators.
Click any operator — then edit the values to see the result update live:
Same value AND same type
Always use === not == — double equals does type coercion and produces surprising results. Triple equals is strict and predictable.
Live condition evaluation
Drag the slider and watch each condition evaluate in real time. Notice that JavaScript stops checking once it finds the first true:
JavaScript checks each condition top to bottom and stops at the first true one. Only one branch ever runs.
Functions that return different values
The pattern you'll use constantly: a function that inspects its input and returns different output based on what it finds.
Common patterns you'll use every day
Your challenge
Write getGreeting(role) — a function that returns a different string for three different roles. Click the animation above with each role to visualise what your code needs to do.
The pattern is: if → else if → else. Three conditions, one function, three possible outputs.
Challenge
Write a function called getGreeting(role) that returns different strings based on role: if role is \"admin\" return \"Welcome to the control panel.\", if role is \"user\" return \"Welcome back!\", otherwise return \"Please sign up to continue.\". Then log the result for all three roles.