Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • JavaScript - Get Started
  • What is JavaScript
  • Setup JavaScript Dev Environment
  • HTML <script> Tag
  • JavaScript - Syntax
  • JavaScript - Popup Message
  • JavaScript - Variables
  • JavaScript - Operators
  • JavaScript - Data Types
  • JavaScript - String
  • JavaScript - Numbers
  • JavaScript - Boolean
  • JavaScript - Object
  • JavaScript - Date
  • JavaScript - Date Methods
  • JavaScript - Array
  • JavaScript - Array Methods
  • JavaScript - null and undefined
  • JavaScript - Function
  • JavaScript - if condition
  • JavaScript - switch
  • JavaScript - for loop
  • JavaScript - while loop
  • JavaScript - Scope
  • JavaScript - eval
  • JavaScript - Error Handling
  • JavaScript - strict mode
  • JavaScript - Hoisting
  • Define JS Class
  • JS Object In Depth
  • this Keyword
  • new Keyword
  • Prototype
  • Inheritance
  • Closure
  • IIFE
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

JavaScript if else Statements

JavaScript includes if/else conditional statements to control the program flow, similar to other programming languages.

JavaScript includes following forms of if-else statements:

  1. if Statement
  2. if else Statement
  3. else if Statement

if Statement

Use if conditional statement if you want to execute something based on some condition.

Syntax:
if(boolean expression)// code to be executed if condition is true
Example: if condition
if( 1 &gt; 0)
{
    alert("1 is greater than 0");
}

if( 1 &lt; 0)
{
    alert("1 is less than 0");
}
Try it

In the above example, the first if statement contains 1 > 0 as conditional expression. The conditional expression 1 > 0 will be evaluated to true, so an alert message "1 is greater than 0" will be displayed, whereas conditional expression in second if statement will be evaluated to false, so "1 is less than 0" alert message will not be displayed.

In the same way, you can use variables in a conditional expression.

Example: if condition
var mySal = 1000;
var yourSal = 500;

if( mySal &gt; yourSal)
{
    alert("My Salary is greater than your salary");
}
Try it
 

curly braces is not required when if block contains only a single line to execute.

Use comparison operators carefully when writing conditional expression. For example, == and === is different.

Example: if condition
if(1=="1")
{
    alert("== operator does not consider types of operands");
}

if(1==="1")
{
    alert("=== operator considers types of operands");
}
Try it

else condition

Use else statement when you want to execute the code every time when if condition evaluates to false.

The else statement must follow if or else if statement. Multiple else block is NOT allowed.

Syntax:
if(condition expression)//Execute this code..else//Execute this code..
Example: else condition
var mySal = 500;
var yourSal = 1000;

if( mySal &gt; yourSal)
{
    alert("My Salary is greater than your salary");
}
else
{
    alert("My Salary is less than or equal to your salary");
}
Try it

else if condition

Use "else if" condition when you want to apply second level condition after if statement.

Syntax:
if(condition expression)else if(condition expression)
Example: else if condition
var mySal = 500;
var yourSal = 1000;

if( mySal &gt; yourSal)
{
    alert("My Salary is greater than your salary");
}
else if(mySal &lt; yourSal)
{
    alert("My Salary is less than your salary");
}
Try it

JavaScript allows multiple else if statements also.

Example: Multiple if else conditions
var mySal = 500;
var yourSal = 1000;

if( mySal &gt; yourSal)
{
    alert("My Salary is greater than your salary");
}
else if(mySal &lt; yourSal)
{
    alert("My Salary is less than your salary");
}
else if(mySal == yourSal)
{
    alert("My Salary is equal to your salary");
}
Try it

We will learn about switch case in the next section.

Points to Remember :
  1. Use if-else conditional statements to control the program flow.
  2. JavaScript includes three forms of if condition: if condition, if else condition and else if condition.
  3. The if condition must have conditional expression in brackets () followed by single statement or code block wrapped with .
  4. 'else if' statement must be placed after if condition. It can be used multiple times.
  5. 'else' condition must be placed only once at the end. It must come after if or else if statement.
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.