JavaScript

JavaScript If Else Statement || JavaScript

javascript if else

JavaScript If else use in order to handle conditions.So that we can execute different actions in different situations. If we want to make a condition and want that our  statement will only be run in a situation when the desired condition is fulfilled. In that case we use the Javascript If else statement to fulfill such condition.

Syntax:

if(condition_1){
// execute when condition_1 is true 
} else if(condition_2){
// execute when condition_1 is false but condition_2 is true
}else{
//execute when both condition_1 and condition_2 is false
}

Example #1:

Find 100 is less than 1000 using if else statement.

<script>
var num=100;
if(num>1000){
   console.log("100 is greater than 1000");
}else{
   console.log("100 is less than 1000");
}
</script>

Output:

100 is less than 1000

Logical Operators:

In JavaScript the Logical Operates are such as:

Operator NameJavaScript Logical Operator
AND&&
OR||
NOT!

Example #2:

Find the Grade of student,As A+(90-100) , A(80-90) , B(70-80) ,C(60-70) ,D(50-60), F(0-50).

<script>

var grade=85;
if(grade>=90 & grade<100){
    console.log("Grade A+");
}else if(grade>=80 & grade<90){
    console.log("Grade A");
}else if(grade>=70 & grade<80){
    console.log("Grade B");
}else if(grade>=60 & grade<70){
    console.log("Grade C");
}else if(grade>=50 & grade<60){
    console.log("Grade D");
}else{
    console.log("Grade F");
}
</script>

Output:

Grade A

Example #3:

Search “Apple” in fruits array such as “Mango”,”Banana”etc. Display result in console.

<script>

var fruits=["Apple","Banana","Mango","PineApple","Orange"];
var item="Apple";

var counter=0;

for(var i=0; i<fruits.length ;i++){

    if(item==fruits[i]){
        counter=1;
    }
}
if(counter==1){
    console.log("Apple is in fruits array");
}else{
    console.log("Apple is not in fruits array")
}
</script>

Output:

Apple is in fruits array

Nested if else:

In order to make nested if else statements,write if else statements under if else statements.for example:

if(condition){
     if(another condition){
     }else{
         if(another condition){
             .......
         }

     }
}else{
    if(another condition){
     }else{
         if(another condition){
             .......
         }

     }
}

Example #3:

Find the maximum number from array.

var num=[10,20,30];

if(num[0]>num[1]){
    if(num[0]>num[2]){
        console.log("Max Number ="+num[0]);
    }else{
        if(num[2]>num[1]){
            console.log("Max Number ="+num[2]);
        }
    }

}else{
    if(num[1]>num[2]){
        console.log("Max Number ="+num[1]);
    }else{
        if(num[2]>num[0]){
            console.log("Max Number ="+num[2]);
        }
    }
}

Output:

Max Number =30

Break Statement:

In order to break the loop we use JavaScript break statement.

Example #4

Find only 3 even number from loop using if else.

var counter=0;
for(var i=0;i<10;i++){
    if(i%2==0){
        counter++;
        console.log(i)
    }
    if(counter==3){
        break;
    }  
}

Output:

0
2
4

Note: There are many Compilers you can use to run  the HTML.CSS,JAVASCRIPT and PHP codes like Brackets, visual studio and Atom . The best and recommended editor is Visual Studio if you have a System with Sufficient requirements.

For more Examples Related CSS,html and Java Script Visit on Coding Academics

Leave a Reply

Your email address will not be published. Required fields are marked *