Java Script


JavaScript Tutorial


Admission Enquiry Form


What is If Else If Statement in JavaScript?

If statement is conditional statement in JavaScript.It evaluates the if.....else if block only if expression is true from many expressions.

Syntax of If Else If Statement...

if(condition1)
  {
  //block will be executed if condition 1 is true
  }
else if(condition2)
  {
//block will be executed if condition 2 is true
  }
  else if(condition3)
  {
//block will be executed if condition 3 is true
  }
  else
  {
//block will be executed if not any condition is true
  }

Example of If Else If Statement Using prompt() function:

<html>
<head>
<title>if else if statement</title>
</head>
<body>
<script>
var num;
num=parseInt(prompt("Enter a number "));
if(num>0)
{
document.write("number is positive");
}
else if(num<0)
{
document.write("number is negative");
}
else
{
document.write("number is zero");
}
</script>
</body>
</html>

Output

Enter a number 0
number is zero


In the above example, if the user enters any positive number, 'If block' will get executed , if user enters any negative number then 'Else If ' block will get executed and if user enters 0 'Else' block will get executed.
Here, we are using prompt() function to take the input from the user.You can also see that we have used parseInt() function with prompt() function.prompt() function takes input from the user as text.If we have to covert this value into integer ,then parseInt() function is used.After converting value into integer then it will be assigned to variable num.