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
if
statement. - If the condition is true, the block of code inside the
if
statement runs. - If the condition is false, the block of code inside the
else
statement runs instead.
Detailed Example – Learn about JavaScript ELSE STATEMENTS
How It Works
- When the user clicks the button, the
checkAccess
function is triggered. - The
prompt
function 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
if
condition is evaluated for truthiness. If it’s true, the associated code block executes. - Fallback Mechanism: If the
if
condition is false, theelse
block 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
else
keyword is used immediately after anif
statement. - Logic Flow:
- If the
if
condition is true, the code in theif
block executes. - If the
if
condition is false, the code in theelse
block executes.
- If the
- Curly Braces: Both
if
andelse
statements use curly bracesto define their respective blocks of code.
How Does It Work?
- The browser evaluates the condition in the
if
statement. - If true, the
if
block runs and theelse
block is ignored. - If false, the browser skips the
if
block and runs theelse
block.
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!