If Conditions — Complete Guide
If Conditions — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of JavaScript Tutorial on Toolliyo Academy.
On this page
JavaScript Tutorial · Lesson 11 of 100
If Conditions
Basics → Objects & data → Async & DOM → Advanced → Tools → Projects
Beginner · 1 — Learn by example · ~6 min · JS Control Flow & Functions
What is this?
if checks a condition. When it is true, the code inside { } runs. else runs when the condition is false. You use this for every decision in an app.
Why should you care?
Show a discount only if total > 1000. Show an error only if the password is wrong.
See it live — copy this example
Paste into an HTML file or the browser console (F12). Use Run below when the live editor is available.
let age = 20;
if (age >= 18) {
console.log("You can vote.");
} else {
console.log("Too young to vote.");
}
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- age >= 18 is the condition.
- The first block runs when true, else runs when false.
- Conditions use comparison operators from the Operators lesson.
Practice next
- Run the age example with age 20 and 15.
- Add else if (age >= 16) for a learner permit message.
- Try if ("hello") — non-empty strings are truthy.
- Combine two conditions with && for "can vote AND has ID".
- Replace the else branch with a ternary assigned to a variable.
Remember
if (condition) { ... } else { ... } for the other case else if for extra branches
ScriptVerse KYC gate
A banking app shows transfer UI only if user.kycStatus === "verified" and balance > 0.
Outcome: Conditional UI prevents illegal actions before they reach the server.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!