MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Conditional Statements: if, else if, else0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
4.1Conditional Statements: if, else if, else4.2Switch Statements4.3Truthy & Falsy Values
HomeTutorialsJavaScript for BeginnersConditional Statements: if, else if, else
Switch Statements

Conditional Statements: if, else if, else

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Programs must make decisions based on real-world inputs: whether a user is logged in, whether an item is in stock, or whether a payment succeeded. Conditional statements (if, else if, else) control the execution flow of your code based on boolean conditions.


1. Syntax of Conditional Blocks

1if (condition1) {
2 // Executes if condition1 is truthy
3} else if (condition2) {
4 // Executes if condition1 was falsy AND condition2 is truthy
5} else {
6 // Executes if ALL preceding conditions were falsy
7}

Real-World Example: Grading System

1function evaluateStudentMarks(marks) {
2 if (marks >= 90) {
3 return "Grade A+ (Distinction)";
4 } else if (marks >= 75) {
5 return "Grade A (First Class)";
6 } else if (marks >= 50) {
7 return "Grade B (Second Class)";
8 } else if (marks >= 35) {
9 return "Grade C (Pass)";
10 } else {
11 return "Fail (Needs Improvement)";
12 }
13}
14 
15console.log(evaluateStudentMarks(82)); // "Grade A (First Class)"

2. Execution Mechanics: Order Matters!

JavaScript evaluates else if conditions sequentially from top to bottom:

  • As soon as one condition evaluates to true, JavaScript executes that block and skips all remaining else if and else blocks completely!

❌ Common Trap: Incorrect Condition Order

1// BUGGY: 95 will match >= 50 first and return Grade B!
2if (marks >= 50) {
3 return "Grade B";
4} else if (marks >= 90) {
5 return "Grade A"; // Unreachable for 90+!
6}

3. Nested if Statements vs. Logical Operators

You can nest if statements inside one another, but combining conditions with && or || creates cleaner code:

Nested (Cluttered):

1if (user.isLoggedIn) {
2 if (user.role === "admin") {
3 showAdminDashboard();
4 }
5}

Flattened with && (Clean):

1if (user.isLoggedIn && user.role === "admin") {
2 showAdminDashboard();
3}

4. The Guard Clause Pattern (Early Return)

In professional codebases, avoid deeply indented "pyramid of doom" conditionals. Use Guard Clauses to check for invalid conditions and return early:

1// Professional Guard Clause Pattern:
2function processPayment(cart, user) {
3 if (!user.isLoggedIn) {
4 return { success: false, error: "Please log in to continue" };
5 }
6 
7 if (cart.items.length === 0) {
8 return { success: false, error: "Cart is empty" };
9 }
10 
11 if (user.balance < cart.total) {
12 return { success: false, error: "Insufficient funds" };
13 }
14 
15 // Happy path code stays un-nested at the bottom!
16 return completeTransaction(cart, user);
17}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What happens when an if condition evaluates to false and there is an else block?
Question 2
What happens if two different else if conditions are both mathematically true in the same chain?
Question 3
What is the "Guard Clause" pattern in clean coding?
Question 4
Which logical operator should be used if you want a block to execute only when BOTH user.isActive AND user.hasSubscription are true?
Question 5
Can an if statement exist without an else block?
Next Lesson

Switch Statements

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Assignment & Ternary OperatorSwitch Statements

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext