Javascript if else else if - code bug free

javascript if else and else if

We discuss about the if else in javascript. Javascript is the official language for web. If you don't know the use of javascript than you come in the right place. Today we will discuss the if else in javascript. Mainly beginner confuse in Java and java script but Java and javascript is the different language. When java script is invented that time most famous programming language is Java. So the javascript developer make the javascript programming name.

Lot only the difference is same in java and javascript. Most of syntax is different in javascript. All of the browser like google chrome excuted the Javascript. if you make any website without javascript than that language convert the javascript when the website is loading. Browser automatically convert other language to javascript language.

Syntax  

var age = 5;
if (age > 7){
    console.log("You can visit our website code bugfree");
}
else{
    console.log("you are too small.");
}

It is the syntax of the if else in javascript. First we create a age variable. And compaire with javascript if else. Here if condition not meet and print in console is else.

INOUTPUT

you are too small.

Multiple line of if

Now we see the multiple line of if. Here if matching the all if condition. One by one. One condition of if is match than slaso se the matching other if condition. See in code.

var favroite_number = 40;
if (favroite_number > 50){
    console.log("My favroite number is not a 50");
}
if (favroite_number == 20){
    console.log("my favroite number is not equal to 20");
}
if (favroite_number > 20){
    console.log("yes my favroite number is greater than 20");
}
if (favroite_number == 40){
    console.log("you are correct you choose the my favroite number");
}
if (favroite_number == 67){
    console.log("you say 67 it isa totally wrong");
}
else{
    console.log("you are fail.")
}

It is multiple if condition. Here program match the program one by one except last if condition Because last if is define with else. We make the variable favroite number 40. All condition trying to guess our favroite number. Wrong condition is ignore by program and write condition is print in any browser console.

INOUTPUT

yes my favroite number is greater than 20
you are correct you choose the my favroite number
you are fail. 

It is the output of multiline if. There two condition is match and print. Last if is not match and else conduition is print.

Javascript in if else if

Javascript else if is use when there is mathch one condition but you write many condition. When one condition is meet than automatic ignore the other condiotion. So the programmer use else if. It is similar like the If condition and else condition.

  

var roll_num = 6;
if (roll_num > 8){
    console.log("sorry my dear friend you is wrong");
}
else if (roll_num == 5){
    console.log("You are also wrong.");
}
else if (roll_num > 2 ){
    console.log("you are right my roll num is greater than 2");
}
else if (roll_num == 6){
    console.log("you are correct but already other gues");
}
else{
    console.log("you are totally wrong");
}

Here two condition is match but only first condition is print. We make the roll number as a variable. matching the all condition with the roll number. Two is matching firat is print in browser console.

INOUTPUT

you are right my roll num is greater than 2

Post a Comment

0 Comments