HOME       >>       Programming

Conditional Statements Of Javascript Tutorial for beginers


Tourist

In this tutorial we will discuss about conditional statements of JavaScript. JavaScript has 4 types of conditional statements such as if statement, if...else statement, if...else if....else statement and switch statement. All this statements are used in different condition. Now we will try to discuss this 4 types of statements with some examples.

When we need to execute some code only in a specified condition, then we will use if statement.
The syntax of if statement is
if (condition)
{
code to be executed if condition is true
}
One important thing is that this if will be in small letter, if we use in capital letter it will show a JavaScript error.

Let we want that if password is “11111” it will show “Password Accepted”. So, the condition will be password==11111, and the complete code will be

<script type="text/javascript">var password=11111;if (password==11111) {document.write("<b> Password Accepted </b>");}</script>


When we need to execute some code if the condition is true and another code if the condition is false, then we will use if....else statement.
The syntax of if....else statement is
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}

Let we want that if password is “xyzabc” it will show “Password Accepted” otherwise it will show “You do not have permission to enter”. So, the condition will be password==11111, and the complete code will be
<script type="text/javascript">var password=11111;if (password==11111) {document.write("<b> Password Accepted </b>");}else{document.write("<b> You do not have permission to enter </b>");}</script>



When we need to execute select one of many blocks of code to be executed then we will use if...else if....else statement.
Syntax of if...else if....else statement is
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else if (condition3)
{
code to be executed if condition3 is true
}
....
....
....
else if (condition n)
{
code to be executed if condition n is true
}

else
{
code to be executed if all conditions are not true
}

For an example, let we want that it will show Good Morning before 10 AM, Good Day in between 10 AM and 4 PM and all other time it will show only Welcome!. So the first condition is time<10, second condition is time>10 && time<16. So, the complete code will be
<script type="text/javascript">var d = new Date()var time = d.getHours()if (time<10){document.write("<b>Good morning</b>");}else if (time>10 && time<16){document.write("<b>Good day</b>");}else{document.write("<b>Welcome!</b>");}</script>

We use switch statement when we want to execute select one of many blocks of code to be executed.
Syntax of switch statement is
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
case 3:
execute code block 3
break;
......
......
......
default:
code to be executed if n is different from all cases
}

For an example, let we ant to know what day today is. We know that 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday and 6 = Saturday. So, the complete code will be

<script type="text/javascript">var d=new Date();theDay=d.getDay();switch (theDay){case 0: document.write("Sunday"); break;case 1: document.write("Monday"); break;case 2: document.write("Tuesday"); break;case 3: document.write("Wednesday"); break;case 4: document.write("Thursday"); break;case 6: document.write("Saturday"); break;default: document.write("Is it my Holyday!");}</script>


Jeigh1405241495

This could be a very useful tutorial for someone just starting out into programming. These conditional statements are pretty standard fair and are pivotal to a programmers arsenal be it in javascript or other languages. While the syntax often changes in slight ways the basic concepts stay the same so hopefully some people just starting to look at coding stumble onto this tut.


iGuest

some conditional statements not working in IE but working in chromeConditional Statements Of Javascript

I have developed a web based library management system using jsp. It also uses javascript for validation and other such purposes. My problem is that when I run the application in IE, a lot of conditional statements of javascript do not work and the validation is not done. While in Google Chrome, the same code works perfectly.

In IE though all other parts like alert boxes, prompt boxes,etc. Work except the conditional statements.

Can u please help me on this...

-question by Abhijeet

 



VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.