DONATE  
MENU
AJAX
JAVASCRIPT
HTML
JSON

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Conditional Testing » If Else Statement
Share Tweet WhatsApp

If Else Statement

IF WITH ELSE STATEMENTS The else keyword of the "if" statement is used in indicating a code block to execute when the if statement condition(s) returns false. It makes an if statement complete by specifying a statement(s) to execute when the tested condition is false.
if (admin){
alert("ACCESS GRANTED.");
}
else
{
alert("ACCESS DENIED.");
}
With the else keyword, you can now specify statements to execute when a condition is false.
<script>
var password = prompt("Enter a password:");
if (password == 123){
alert("LOGIN SUCCESSFUL"); //What to execute when statement is true
}
else{
alert("INCORRECT PASSWORD"); //What to execute when statement is false
}
</script>

Copy PRACTICE IT

In the above, a user enters a password, if it matches our condition, then the "login successful" string is alerted else an alert of "incorrect password". ELSE IF STATEMENT The Else If statement is used in extending the if statement. That is, with the else if statement, we can specify more conditions to be tested when the "if" is false. For example, "if Anita's age is less than 12, then Anita is a child else if Anita's age is less than 18, Anita is a teenager else if Anita's age is less than 50, Anita is a youthful adult else Anita is an adult."
if (age < 12){
alert("Child's Age");
}
else if (age < 18){
alert("Teenager's Age");
}
else{
alert("Adult's Age");
}
<script>
var age = prompt("Enter the age:");
if (age < 12){
alert("Child's Age");
}
else if (age < 18){
alert("Teenager's Age");
}
else if (age < 40){
alert("Adult's Age");
}
else{
alert("Full Adult's Age");
}
</script>

Copy PRACTICE IT

You can have as many else if statements as you want. NESTED IF STATEMENT You can nest an if, if else statements in an if statement. In simpler terms, you can have other if statements inside an if statement . EXAMPLE
if (user_login == true){

if (user_role == "admin"){

if (admin_role == "author"){
alert("Welcome Author");
}

}

}
else{
alert("Welcome");
}
Above we can see other if statements nested inside an if statement. The main if statement tests if a user is login. If true, the if statement inside of it test if the role of the logged in user is that of an "admin" and if true again, the if statement inside of it tests again if the admin role is that of an "author" before displaying a message. You can nest as much if statements, if with else statements, if else statements as you want in an if statement, if with else statement or if else statement. SUMMARY
  1. The if statement can have an else keyword to specify a statement(s) to execute when its condition returns false.
  2. We can also test more than one condition using the if else statement.
  3. If an if statement is extended to test more than one condition using the else if, it is good practice to also have an else keyword.
  4. We can nest an if statement or if with else statement or else if statement in an if statement or if with else statement or if else statement .
Take a short quiz on If Else Statement.
Ask a question or tell us if If Else Statement is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode