If else if statement in Java Script with Examples

Updated: Aug 31




If Statement: If statement is execute when condition is true. If condition is not satisfied then statement is not execute. It means we get result when condition is satisfied and condition is not satisfy then result is not show.

Syntax:

If (Condition)

statement;


Program:

<html>

<body>

<h2>Enter Age</h2>

<input id="age"><br><br>

<button onclick="myfun()">check my age </button>

<br><br>

<script type="text/javascript">

function myfun()

{

var age=document.getElementById('age').value;

if(age>18)

{

document.write("Elder");

}

}

</script>

</body>

</html>

Check Output:





If and Else statement :

If and else statement is also conditional statement. In this statement when condition is true then program is execute and condition is not satisfied then statement is execute. It means we get result when condition is satisfied and condition is not satisfy then result also show.

Syntax:

If (Condition)

statement;

else

statement;


Program:

<html>

<body>

<h2>Enter Age</h2>

<input id="age"><br><br>

<button onclick="myfun()">check my age </button>

<br><br>

<script type="text/javascript">

function myfun()

{

var age=document.getElementById('age').value;

if(age>18)

{

document.write("Elder");

}


else

{

document.write("child");

}

}

</script>

</body>

</html>




If else if statement in Java Script; We use this statement when we need more than two condition.

Syntax:

If (Condition)

statement;

else if (condition)

statement;

else

statement;

Program:

<html>

<body>

<h2>Enter Age</h2>

<input id="age"><br><br>

<button onclick="myfun()">check my age </button>

<br><br>

<script type="text/javascript">

function myfun()

{

var age=document.getElementById('age').value;

if(age>18)

{

document.write("Elder");

}

else if(age==18)

{

document.write("Younger");

}

else

{

document.write("child");

}

}

</script>

</body>

</html>

Output:



Write a program in Javascript and enter valid marks? Use following condition:

>=80 then Result ="First Grade"

>=60 then Result ="Second Grade"

>=35 then Result ="Third Grade"

<35 then Result ="Fail"

Program:

<!doctype html>

<html>

<body>

<h2>Enter Students Marks</h2>

<input id="m"><br>

<button onclick="checkresult()">Check</button>

<br>

<h1><span id="text"></span></h1>

<script>

function checkresult()

{

var marks=document.getElementById("m").value

if(marks>=80)

{

document.getElementById('text').innerHTML='First grade';

}

else if(marks>=60)

{

document.getElementById('text').innerHTML='Second grade';

}


else if(marks>=35)

{

document.getElementById('text').innerHTML='Third grade';

}

else

{

document.getElementById('text').innerHTML='Fail';

}

}

</script>

</body>

</html>


Output:




144 views0 comments

Recent Posts

See All

Switch Statement: A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case and the variable being switched on is checked for each switch cas

CSS stands for Cascading Style Sheet. A CSS files allows us to style for HTML Content. We can present the content on our website with fonts, colors, background, borders, text formatting, links effect