Approx. read time: 3.1 min.
Post: Learn about JavaScript ELSE STATEMENTS
Lesson: JavaScript else Statements
Introduction to else Statements
The else statement is used in conjunction with an if statement to specify an alternative block of code that executes when the if condition is false. This adds a layer of logic that allows developers to handle different scenarios in their web applications.
Syntax
Here’s the flow:
- The browser evaluates the condition in the
ifstatement. - If the condition is true, the block of code inside the
ifstatement runs. - If the condition is false, the block of code inside the
elsestatement runs instead.
Detailed Example – Learn about JavaScript ELSE STATEMENTS
How It Works
- When the user clicks the button, the
checkAccessfunction is triggered. - The
promptfunction asks the user to enter a username. - If the username entered is “Admin”, the browser shows an alert saying “Access Granted!”.
- Otherwise, the browser shows “Access Denied!”.
Key Concepts – Learn about JavaScript ELSE STATEMENTS
- Condition Evaluation: The
ifcondition is evaluated for truthiness. If it’s true, the associated code block executes. - Fallback Mechanism: If the
ifcondition is false, theelseblock acts as a fallback. - Equality Operator (
===): This ensures strict comparison, checking both value and type.
More Examples
Example 1: Checking Age
Example 2: Determining Even or Odd
Assignment – Learn about JavaScript ELSE STATEMENTS
Question 1: Write a Program
Write a program using if and else statements that:
- Prompts the user to enter a password.
- If the password matches
"secret", display an alert saying"Login Successful!". - If the password is incorrect, display an alert saying
"Incorrect Password. Try Again.".
Answer Key – Learn about JavaScript ELSE STATEMENTS
Here’s the solution to the assignment:
Practice and Apply
To solidify your understanding, try these tasks:
- Write a program that checks if a number is positive, negative, or zero.
- Create a page where users can enter their grade. If the grade is above 70, show “You passed!”. Otherwise, show “You failed!”.
Summary: Understanding JavaScript else Statements
What are else Statements?
Else statements work alongside if statements to handle alternative scenarios in your code. When the condition in an if statement evaluates to false, the browser runs the code inside the else block instead.
Key Points to Remember
- Syntax: The
elsekeyword is used immediately after anifstatement. - Logic Flow:
- If the
ifcondition is true, the code in theifblock executes. - If the
ifcondition is false, the code in theelseblock executes.
- If the
- Curly Braces: Both
ifandelsestatements use curly bracesto define their respective blocks of code.
How Does It Work?
- The browser evaluates the condition in the
ifstatement. - If true, the
ifblock runs and theelseblock is ignored. - If false, the browser skips the
ifblock and runs theelseblock.
Real-World Examples
- Login Validation: Show "Login Successful!" if a password matches; otherwise, show "Incorrect Password."
- Access Control: Grant or deny access based on the user's role.
- Even or Odd Numbers: Display whether a number is even or odd.
Why Use else Statements?
- To handle alternative outcomes for a condition.
- To make web pages dynamic and interactive by responding differently based on user input or system state.
- To simplify code by grouping related conditions logically.
Practice Assignment
- Write a program where users enter data (e.g., username, age, or password), and the browser responds differently based on whether the input meets specific conditions.
By mastering else statements, you'll gain foundational skills to handle decision-making in your JavaScript programs!



