⚡ Rocket.net – Managed WordPress Hosting

MiltonMarketing.com  Powered by Rocket.net – Managed WordPress Hosting

Bernard Aybouts - Blog - MiltonMarketing.com

Approx. read time: 3.1 min.

Post: Learn about JavaScript ELSE STATEMENTS

Copy to Clipboard

Here’s the flow:

  1. The browser evaluates the condition in the if statement.
  2. If the condition is true, the block of code inside the if statement runs.
  3. If the condition is false, the block of code inside the else statement runs instead.

Detailed Example – Learn about JavaScript ELSE STATEMENTS

Copy to Clipboard

How It Works

  1. When the user clicks the button, the checkAccess function is triggered.
  2. The prompt function asks the user to enter a username.
  3. If the username entered is “Admin”, the browser shows an alert saying “Access Granted!”.
  4. 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, the else block acts as a fallback.
  • Equality Operator (===): This ensures strict comparison, checking both value and type.

More Examples

Example 1: Checking Age

Copy to Clipboard

Example 2: Determining Even or Odd

Copy to Clipboard

Assignment – Learn about JavaScript ELSE STATEMENTS

Question 1: Write a Program

Write a program using if and else statements that:

  1. Prompts the user to enter a password.
  2. If the password matches "secret", display an alert saying "Login Successful!".
  3. 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:

Copy to Clipboard

Practice and Apply

To solidify your understanding, try these tasks:

  1. Write a program that checks if a number is positive, negative, or zero.
  2. 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

  1. Syntax: The else keyword is used immediately after an if statement.
  2. Logic Flow:
    • If the if condition is true, the code in the if block executes.
    • If the if condition is false, the code in the else block executes.
  3. Curly Braces: Both if and else statements use curly braces to 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 the else block is ignored.
  • If false, the browser skips the if block and runs the else 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!

About the Author: Bernard Aybout (Virii8)

Avatar of Bernard Aybout (Virii8)
I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀