Javascript If Else

If Statement:

Sometimes we reqiure to execute certain statement when specified condition true.

Syntax

<script type="text/javascript">
if (condition)
{
   //code that will run if the condition is true
}
</script>

Example

<!DOCTYPE html>
<html>
<head>
<title>My first JSON Object Code</title>
<SCRIPT LANGUAGE="JavaScript">
var age = 18
if (age < 20)
{
   document.write('Invalid person for test');
}
 </SCRIPT>
 </HEAD>
<BODY>
</BODY>
</HTML>
Try Now

If..Else Statement:

When specified condition fails the else statement will get execute.

Syntax

<script type="text/javascript">
if (condition)
{
   //code that will run if the condition is true
}
else
{
  //code that will run if the condition is false
}
</script>

Example

<!DOCTYPE html>
<html>
<head>
<title>My first JSON Object Code</title>
<SCRIPT LANGUAGE="JavaScript">
var age = 18
if (age < 20)
{
   document.write('Invalid person for test');
}
else
{
   document.write('Valid person for test');
}
 </SCRIPT>
 </HEAD>
<BODY>
</BODY>
</HTML>
Try Now

If..Else If Statement

When there are situations when we need to test several conditions then we will use else if condition.

Syntax

<script type="text/javascript">
if (condition1)
{
   //code that will run if the condition1 is true
}
else if(condition2)
{
  //code that will run if the condition2 is true
}
else if(condition3)
{
  //code that will run if the condition3 is true
}
else
{
  //code that will run if the all contitions fails
}
</script>

Example

<!DOCTYPE html>
<html>
<head>
<title>My first JSON Object Code</title>
<SCRIPT LANGUAGE="JavaScript">
var number = -1;
if (number > 0)
{
   document.write('Positive number');
}
else if(number < 0)
{
   document.write('Negetive number');
}
else
{
document.write('Not a number');
}
 </SCRIPT>
 </HEAD>
<BODY>
</BODY>
</HTML>
Try Now

See Also

Javascript Built in String Object
Javascript Built in Number Object
Javascript Built in Math Object
Javascript Built in Date Object
Javascript Built in Arrays Object
Javascript Built in Boolean Object
Javascript Built in RegExp Object
Javascript Built in JSON Object


 
SHARE THIS IF YOU LIKE!
submit to reddit
SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.