If Statements in JavaScript

Ratings:
(4)
Views: 551
Banner-Img
Share this blog:

 

JavaScript if…else Statements

While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make use of conditional statements that allow your program to make the correct decisions and perform the right actions.

JavaScript supports conditional statements that are used to perform different actions based on different conditions. Here we will explain if… else statement JavaScript supports the following forms of if…else statement:

  • if statement
  • if… else statement
  • if….. else if….. statement
Learn JavaScript by Tekslate - Fastest growing sector in the industry. Explore Online "JavaScript Training" and course is aligned with industry needs & developed by industry veterans. Tekslate will turn you into JavaScript Expert.

  If statement

The if statement if the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally.

Syntax

if(expression)

{ statement(s) to be executed if expression is true }

Here JavaScript expression is evaluated. If the resulting value is true, a given statement(s) are executed. If the expression is false then no statement would be not executed. Most of the times you will use comparison while making decisions

  Example

<script type=”text/javascript”>

<!— var age=20; if(age>18)

{ document.write(“<b>Qualifies for driving </b>”); }

//-- >

</script>

This will produce the following result: Qualifies for driving

  if….else statement

The if…else statement is the next form of control statement that allows JavaScript to execute statements in a more controlled way.

Syntax

if  (expression)

{ statement(s) to be executed if expression is true }

else

{ statement(s) to be executed if expression is false }

Here JavaScript expression is evaluated, if the resulting value is true, given statement(s) in the if block, are executed. If expression is false then given stement(s) in the else block, are executed.

 

Check Out JavaScript Tutorial

  Example

<script   type=”text/javascript”>

<!— var age= 15; if(age>18)

{ document.write(“<b> Qualifies for dividing</b>”); }

else { document.wirte(“<b> Does not qualify for driving </b>”); }

//- - >

</script>

This will produce the following result  

Does not qualify for driving if…else if … statement

The if…. else if… statement is the one level advance form of control statement that allows JavaScript to make correct decisions out of several conditions.  

Syntax

If  (expression 1)

{ statement(s) to be executed if expression 1 is true }

else if (expression 2)

{ statement(s) to be executed if expression 2 is true }

else if (expression 3)

{ Statement(s) to be executed if expression 3 is true }

else { Statement(s) to be executed if no expression is true }

There is nothing special about this code. It is just a series of if statements, where each if is part of the else clause of the previous statement. Statement(s) is executed based on the true condition, if none of the condition is true then else block executed.

  Example 

<script type=”text/javascript”>

<!— var book=”maths”; if(book == “history”)

{ document.wirte(“<b>History Book</b>”); }

else if(book== “economics”)

{ document.write(“<b>Economics Book</b>”); }

else { document.write(“<b>Unknown  Book</b>”); }

// - - >

</script>

This will produce the following result: Maths Book

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.