Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

JavaScript Tutorial
JavaScript Introduction
JavaScript How To
JavaScript Where To
JavaScript Variables
JavaScript If...Else Statement
JavaScript Switch Statement
JavaScript Operator
JavaScript Popup-boxes
JavaScript Function
JavaScript For-Loop
JavaScript While-loops
JavaScript Break Loops
JavaScript For..In Statement
JavaScript Event
JavaScript Try-Catch Statement
JavaScript Throw Statement
JavaScript Onerror
JavaScript Spacial Characters
JavaScript Guideline
JavaScript String
JavaScript Date Object
JavaScript Array
JavaScript Boolean Object
JavaScript Math Object
JavaScript HTML DOM Object
JavaScript Browser Detection
JavaScript Cookies
JavaScript Validation
JavaScript Animation
JavaScript Image Maps
JavaScript Timing Event
JavaScript Create Object
JavaScript Summary

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


JavaScript If...Else Statement
Previous Next

If...Else statement
Conditional statements in JavaScript are used to perform different actions based on different conditions.

Explanation

Conditional statements are used when you want something to happen when a condition is either true or false. You can either use the If or Switch conditional statements. Conditional statements are case sensitive and must be written in lowercase.

If statements are used when you want to check one condition

else if is added after the if statement and allows you to check for another condition.

else is added after the if and else if statement and allows you to do something when all previous conditions fail.




if statement
You should use the if statement if you want to execute some code only if a specified condition is true.

if (condition)
{
code to execute if condition true
}

Example:1

<script type="text/javascript"<

var name = "Vyom";

if (name = "Vyom")
{
document.write("Hello Rajesh");
}
</script<

Example:2

<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10

var d=new Date()
var time=d.getHours()

if (time<10)
{
document.write("Good morning")
}
</script>

Example:3

<script type="text/javascript">
//Write "Lunch-time!" if the time is 11

var d=new Date()
var time=d.getHours()

if (time==11)
{
document.write("Lunch-time!")
}
</script>


You always use two equals signs(==) next to each other (==)!When we comparing variable .

Notice that there is no ..else.. in this syntax. You just tell the code to execute some code only if the specified condition is true.


If...else Statement

If else statement has the same format as of "if" statement. Only additional thing is that an else part is added to if statement. So if the conditions satisfies the statements inside if part will be executed else the statement inside else part will be executed.


Syntax of if..else statement

if (condition)
{
code to execute if condition true
}


Example:4

<script language="javascript">
var a = "1234abc";
if(a == "adcdefa")
{
document.write(" inside if statement ");
}
else
{
document.write(" inside else part of statement ");
}
</script>


Output
inside else part of statement
In the above example the condition is to check if variable 'a' equals (==) "abcdefa". The condition fails as we have assigned a as "1234abc". So the else part is executed.


Example:5

<script language="javascript">
//If the time is less than 10,
//you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.

var d = new Date()
var time = d.getHours()

if (time < 10)
{
document.write("Good morning!")
}
else
{
document.write("Good day!")
}
</script>


If...else if...else Statement
You should use the if....else if...else statement if you want to select one of many sets of lines to execute.

Syntax

if (condition)
{
code to execute if condition true
}
else if (condition)
{
code to execute if 2nd condition true
}
else
{
code to execute if above are false
}


Example:6

<script language="javascript">
var name = "Mary";

if (name = "John"){
document.write("Hello John ");
}
else if (name = "Peter")
{
document.write("Hello Peter");
}
else {
document.write("Hello Stranger");
}
</script>


Say Hello John to John, Hello Peter to Peter and Hello Stranger to anyone else.

Example:7

<script language="javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("Good morning")
}
else if (time>10 && time<16)
{
document.write("Good day")
}
else
{
document.write("Hello World!")
}
</script>



Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previous Next

Keywords: javascript tutorials, javascript code, javascript if else operators, javascript if else script type


HTML Quizes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
XML Quizes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizes
JavaScript Quiz
VBScript Quiz
AJAX Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizes
ASP Quiz
PHP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
.NET (dotnet) Quizes
Microsoft.Net Quiz
XML Web Services Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
Multimedia Quizes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Web Building  Quizes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Java Quizes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Programming Langauges Quizes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Soft Skills Quizes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz

Privacy Policy
Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.