Bernard Aybouts - Blog - Miltonmarketing.com

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

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!”.
For privacy reasons YouTube needs your permission to be loaded. For more details, please see our Privacy Policy – Legal Disclaimer – Site Content Policy.

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!

The Longevity Blueprint: AI-Powered Health Optimization

Current step:1AI-Human Medical Analyzer: Smarter, Personalized Health
2AI-Human Medical Analyzer: Smarter, Personalized Health

> SYS.HEALTH: AI-Human Medical Analyzer_

// Revolutionize Your Diagnostics

Experience the perfect blend of cutting-edge AI precision and expert human care. Our revolutionary analyzer turns your raw health data into personalized, actionable insights tailored just for you.

> INITIALIZING_BIOMETRIC_SCAN...

[+] DATA_INPUT

Securely upload complex health parameters, including lab bloodwork and comprehensive medical history.

[+] PROCESSING

Advanced algorithmic parsing combined with human-level oversight ensures hyper-accurate data interpretation.

[+] OUTPUT_MATRIX

Receive smarter, faster, and truly personalized care strategies to take immediate charge of your health journey.

A name/nickname is required to continue.

> TRANSLATION_MATRIX_ACTIVE...
[ LANG_EN ]
Knowledge Heals, Prevention Protects
[ LANG_HI ]
ज्ञान ठीक करता है, रोकथाम सुरक्षा करती है
[ LANG_ZH ]
知识治愈,预防保护
[ LANG_JA ]
知識は癒し、予防は守る
[ LANG_HE ]
הידע מרפא, המניעה מגנה
[ LANG_AR ]
المعرفة تُشفي، والوقاية تحمي
[ LANG_FR ]
La connaissance guérit, la prévention protège

> SYS.AUTH: Data Processing Consent_

[ AWAITING_AUTHORIZATION ] By providing consent, you allow us to process your uploaded data through our proprietary AI-Human analysis system.

  • [+] SECURE_REVIEW: This ensures your information is carefully reviewed using advanced AI technology and certified professional oversight to deliver personalized health insights.
  • [+] PRIVACY_LOCK: Your privacy is our strict priority. Your data will only be used for this specific diagnostic purpose.

> SYS.UPLOAD: Share Medical Records [OPTIONAL]_

[ USER_CONTROL_ACTIVE ] Uploading your medical records during registration is entirely optional. You can choose to bypass this step and provide data later if it suits your timeline.

You dictate the data flow: share as much or as little as you’re comfortable with, and let us guide you toward better health.

[+] FORMAT_SUPPORT

We accept all file formats, including photos, PDFs, text documents, and raw official medical data.

[+] DATA_YIELD

Increased inputs correlate with higher precision. The more info you share, the better we tailor your personalized insights.

> NEXT_STEPS: Post-Registration Protocol_

Once your registration is complete, a human specialist from our team will personally reach out to you within 3-10 business days. We will discuss your health journey and map out exactly how we can support you.

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. 🚀