Using Conditional statements we can apply different actions on different conditions.
JavaScript provide has four different conditional statements.
1.if statement: This statement is used to execute some specific code when specific condition is true.
Syntax:
if (condition)
{
this code will executed if condition is true
}
Example:
<script type="text/javascript">
//Write a "Good Afternoon" welcome if
//the time is greater than 12
var d=new Date();
var time=d.getHours();
if (time>12)
{
document.write("<b>Good Afternoon</b>");
}
</script>
2.if...else statement: If condition is true than execute some code else execute different code.
Syntax:
if (condition)
{
This code will executed if condition is true
}
else
{
This code will executed if condition is not true
}
Example:
<script type="text/javascript">
//If the time is greater than 12,
//you will get a "Good Afternoon" welcome.
//Otherwise you will get a "Good day" welcome.
var d = new Date();
var time = d.getHours();
if (time > 12)
{
document.write("Good Afternoon");
}
else
{
document.write("Good day");
}
</script>
3.if...else if....else statement: Using this statement we can select one of many blocks of code to be executed.
Syntax:
if (condition1)
{
This code will executed if condition1 is true
}
else if (condition2)
{
This code will executed if condition2 is true
}
else
{
This code will executed if condition1 and
condition2 are not true
}
Example:
<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 Afternoon</b>");
}
else
{
document.write("<b>R4R Welcomes You!</b>");
}
</script>
4.switch statement: Using this statement we can select one of many blocks of code to be executed.
Syntax;
switch(i)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is
different from case 1 and 2
}
Example:
<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5, Saturday=6.
var d=new Date();
theDay=d.getDay();
switch (theDay)
{
case 0:
document.write("It is Sunday");
break;
case 1:
document.write("It is Monday");
break;
case 2:
document.write("It is Tuesday");
break;
case 3:
document.write("It is Wednesday");
break;
case 4:
document.write("It is Thursday");
break;
case 5:
document.write("It is Friday");
break;
case 6:
document.write("It is Saturday");
break;
default:
document.write("Press only numbers from 0 to 6 ");
}
</script>
JavaScript is an scripting language for the client side web development means web pages. It is designed by Brenden Eich comes in 1995.
Developer of JavaScripts are Netscape Communications Corporation, Mozilla Foundation. As name indicate you think it is essential related to Java. Its wrong its syntax matched with C Syntax,it use some java name and their convension. It is influenced by Seff,C,Scheme,Perl,Python,Java. Using this we can do many thimgs like: to improve Design,validate forms,detect browsers etc. JavaScript can be use by new programmer easily. I have given you some most important point related to JavaScript.
JavaScript is a trademark of Sun Microsystem.
It is dynamic,Weak and prototype language.
Using JavaScript we can make our HTML pages interactive.
JavaScript is a scripting language.
We can embedded JavaScript directly into HTML pages.
It is an interpreted language means for execution their is no need to pre-compilation of JavaScript. Latest version of JavaScript is 1.8/ 2008
Java (developed by Sun Microsystems) is a powerful and much more complex programming language in the same category as C and C++.
JavaScriptwas created by Brendan Eich at Netscape and was first introduced in December 1995 under the name of LiveScript. However, it was rather quickly renamed JavaScript, although JavaScripts official name is ECMAScript, which is developed and maintained by theECMA (European Computer Manufacturer\'s Association) International organization.
ECMA Organization developed and maintain ECMAScript.
An official JavaScript standard is ECMA-262.Development to of this started in 1996 and It is first used in ECMA General Assembly in june 1996.
It gets cettificated by international ISO (ISO/IEC 16262) standard in 1998.
It is usually based on JavaScript and Jscript Repectively given by Netscape and Microsoft.First it is invented by Brendan Eich at Netscape and applied on Navigator 2.0 after 1996 it is used on all type of Netscape and micrisoft browser.