codescracker


javascript

JavaScript if-else Statement



« Previous Tutorial Next Tutorial »


The if-else statement in JavaScript is used to execute the code that is written in the if statement, if the specified condition is true. Otherwise, the code written in the else statement is executed.

JavaScript if-else Statement Example

Here is an example demonstrates if-else statement in JavaScript.

<!DOCTYPE HTML>
<html>
<head>
	<title>JavaScript if-else Statement</title>
</head>
<body>

<h2>JavaScript if-else statement Example</h2>
<script type="text/javascript">
	var num = 99;
	document.write("Checking the number...<br/>")
	if((num%2) != 0)
	{
		document.write(num + " is an odd number.");
	}
	else
	{
		document.write(num + " is not an odd number.");
	}
	document.write("<br/>Done!");
</script>

</body>
</html>

Here is the sample output of the above JavaScript example of if-else statement:

javascript if-else statement

In the above example, the code present inside the if statement is executed, because the given condition is true. Let's take another example to execute the code of else statement. To execute the code of else statement, you have to provide wrong condition.

To make the condition wrong, you only have to initialize even number to the variable num. Here is an example:

<!DOCTYPE HTML>
<html>
<head>
	<title>JavaScript if-else Statement</title>
</head>
<body>

<h2>JavaScript if-else statement Example</h2>
<script type="text/javascript">
	var num = 98;
	document.write("Checking the number...<br/>")
	if((num%2) != 0)
	{
		document.write(num + " is an odd number.");
	}
	else
	{
		document.write(num + " is not an odd number.");
	}
	document.write("<br/>Done!");
</script>

</body>
</html>

Here is the sample output of the above JavaScript if-else statement example:

javascript if else example

JavaScript Nested if-else Statement

You are free to define one if-else statement into another, such statements are called as nested if-else statements.

Nested if-else statement are useful in situations when additional checking is required.

JavaScript Nested if-else Statement Example

Here is an example illustrates nested if-else statement in JavaScript:

<!DOCTYPE HTML>
<html>
<head>
	<title>JavaScript Nested if-else Statement</title>
</head>
<body>

<h3>JavaScript Nested if-else statement Example</h3>
<script type="text/javascript">
	
	var mychar = "C";
	document.write("Checking the character...<br/>");
	if(mychar == "A")
	{
		document.write(mychar + " is a vowel.");
	}
	else
	{
		if(mychar == "E")
		{
			document.write(mychar + " is a vowel.");
		}
		else
		{
			if(mychar == "I")
			{
				document.write(mychar + " is a vowel.");
			}
			else
			{
				if(mychar == "O")
				{
					document.write(mychar + " is a vowel.");
				}
				else
				{
					if(mychar == "U")
					{
						document.write(mychar + " is a vowel.");
					}
					else
					{
						document.write(mychar + " is a consonant.");
					}
				}
			}
		}
	}
	document.write("<br/>Done!");
	
</script>

</body>
</html>

Here is the sample output of the above JavaScript Nested if-else Statement example:

javascript nested if else statement

You can also write the above JavaScript code as:

<!DOCTYPE HTML>
<html>
<head>
	<title>JavaScript Nested if-else Statement</title>
</head>
<body>

<h3>JavaScript Nested if-else statement Example</h3>
<script type="text/javascript">
	
	var mychar = "C";
	document.write("Checking the character...<br/>");
	if(mychar == "A")
		document.write(mychar + " is a vowel.");
	else
		if(mychar == "E")
			document.write(mychar + " is a vowel.");
		else
			if(mychar == "I")
				document.write(mychar + " is a vowel.");
			else
				if(mychar == "O")
					document.write(mychar + " is a vowel.");
				else
					if(mychar == "U")
						document.write(mychar + " is a vowel.");
					else
						document.write(mychar + " is a consonant.");
	document.write("<br/>Done!");
	
</script>

</body>
</html>

Here is another way, you can write the above code:

<!DOCTYPE HTML>
<html>
<head>
	<title>JavaScript Nested if-else Statement</title>
</head>
<body>

<h3>JavaScript Nested if-else statement Example</h3>
<script type="text/javascript">
	
	var mychar = "C";
	document.write("Checking the character...<br/>");
	if(mychar == "A")
		document.write(mychar + " is a vowel.");
	else if(mychar == "E")
		document.write(mychar + " is a vowel.");
	else if(mychar == "I")
		document.write(mychar + " is a vowel.");
	else if(mychar == "O")
		document.write(mychar + " is a vowel.");
	else if(mychar == "U")
		document.write(mychar + " is a vowel.");
	else
		document.write(mychar + " is a consonant.");
	document.write("<br/>Done!");
	
</script>

</body>
</html>

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test