Java Script


JavaScript Tutorial


Admission Enquiry Form



What is If statement in JavaScript?

If statement is conditional statement in JavaScript.It executes the 'if block' only when expression is true.

Syntax of If statement...

if(condition)
  {
  //number of statements
  }

Example of if statement:

<html>
<head>
<title>if statement</title>
</head>
<body>
<script>
var num=5;
if(num==5)
{
document.write("Welcome to compuhelp");
}
</script>
</body>
</html>

Output

Welcome to Compuhelp

In the above example, the value of num is 5 and when control check the if statement it will be true and give the above output.


Example of if statement Using prompt() function:

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

Output

Enter a number 4
number is positive


In the above example, if the user enters 4 then the control will come inside If statement and give the above output else it will quit the If statement.