Conditional Statement in Javascript – Learn Javascript Online!

Everywhere conditions are like by default, so you need to choose one of them. Like if you are purchasing a car, you will definitely get a question like should you buy really? If yes then what you will do? And if no then also, what you will do?

Then the same thing when occurs in programming is known as conditional statements. It’s just like a yes or no situation. in which you have to pick anyone.

Each if statement consists of at least two parts that are performed…

decision_making

image credit tutorialpoint.com

What you get from the image???

It’s very simple, first, you have the statement like a question, if your statement is true or condition is true then a set of code is performed and if your condition is not true means false then your program is taking the another way but not same as the true one.

In javascript we have three types of conditions of if-else statement:

  1. if statement
  2. if else statement
  3. if else if statement

If Statement

If statement is a simple form of the statement. If your condition is true then the particular set of a statement will be executed. Otherwise, nothing will happen. e.g.

a=2 and we want to print a line like – “Value of a is 2” then what we have to do? What do you think?

Yeah, we have to simply initiate the if statement but how? Let’s check it out…

If statement syntax goes like:

if(condition or expression)

 { 

statement to be executed...

}

Are you asking me a live example? No problem, I will provide, just hold on.

If statement in Javascript Example - Learn Javascript Online

If statement in Javascript Example

CODE

<html>
 <body>
 <script type="text/javascript">
   var a = 2;
        if( a = 2 ){
                     document.write("Value of variable a is 2");
                   }
 </script>
 </body>
</html>

If Else statement

Well, this statement doesn’t carry much difference but yeah it’s different because it carries else part. If your condition falls true then the true statements will be executed otherwise else part statements will be executed. I think you are still in the confused state. No problem, when you will see the example you will get to know the things well.

e.g. a = 2 and we want to print that if a is equal to 2 then – “a holds the value 2” otherwise “a holds value other than 2”.

Syntax

if (condition){
   Statements to be executed if condition is true
}

else{
   Statements to be executed if condition is false
}

Code execution example is given below:

If Else statement in Javascript Example - Learn Javascript online

If Else statement in Javascript Example

CODE IS HERE

<html>
 <body>
 <script type="text/javascript">
 var a = 4;
 if( a == 2 ){
      document.write("variable a hold value");
       }
 else{
 document.write("variable a holds value other than 2");
 }
 </script>
 </body>
</html>

If-Else-if statement

If else if statement is the advanced version of if else. Asking me how? Okay, let me tell you that. Because it provides you the several else statement execution if anyone is not satisfied.

Syntax

The syntax of an if-else-if statement is as follows −

if (condition 1){
   Statements to be executed if condition 1 is true
}

else if (condition 2){
   Statements to be executed if condition 2 is true
}

else if (condition 3){
   Statements to be executed if condition 3 is true
}

else{
   Statements to be executed if no condition is true
}

I am here providing you a demo program that will definitely help you to grab more details.

If Else if statement in Javascript Example - Learn Javascript online

If Else if statement in Javascript Example

CODE IS HERE

<html>
 <body>
 
 <script type="text/javascript">
 
 var my_name = "codingparks";
 if( my_name == "codingparks" ){
 document.write("<b>You are using Codingparks.com</b>");
 }
 
 else if( my_name == "quickbodyfitness" ){
 document.write("<b>You are using Quickbodyfitness.com</b>");
 }
 
 else if( my_name == "lpu" ){
 document.write("<b>You are using lpu.com</b>");
 }
 
 else{
 document.write("<b>Unknown Website</b>");
 }
 
 </script>
</body>
<html>

Okay, so this is all about the statements session.

Leave a Reply

CONTACT ME