Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Essential Javascript -- A Javascript Tutorial

By Patrick Hunlock
2007-12-02


JavaScript Conditionals: IF

The if statement lets you execute a block of code if some test is passed.

var x=5;
if (x==5) {
   alert
('x is equal to 5!');
}

You can also use an else clause to execute code if the test fails.

var x=5;
if (x==5) {
   alert
('x is equal to 5!');
} else {
   alert
('x is not equal to 5!');
}

An elseif statement also exists which allows for better formating of long conditional tests.

var x=5;
if (x==1) {
   alert
('x is equal to 1!');
} else if (x==2) {
   alert
('x is equal to 2!');
} else if (x==5) {
   alert
('x is equal to 5!');
} else {
   alert
('x isn't 1, 2 or 5!');
}



Tutorial Pages:
» Essential Javascript -- A Javascript Tutorial
» Getting Started
» In-Line Javascript
» External Javascript
» Javascript is case sensitive
» Output (writeln)
» Output (alert)
» Output (getElementById)
» Input (One Click To Rule Them All)
» Input (User Input)
» Javascript is an Event Driven Language
» Comments
» Variables
» Variable Scope
» Special Keywords
» Arithmetic Operators
» Logical and Comparison Operators
» JavaScript Conditionals: IF
» JavaScript Conditionals: SWITCH
» JavaScript Conditionals: Shorthand Assignment
» JavaScript Conditionals: Ternary Operators
» JavaScript Loops: FOR
» JavaScript Loops: FOR/IN
» JavaScript Loops: WHILE
» Bringing It All Together
» DHTML: Dynamic HTML
» Conclusion


copyright © 2006, 2007 by Patrick Hunlock


 | Bookmark
Related Tutorials:
» JavaScript Debugging Techniques with Firebug
» Striped Tables Using JavaScript
» Opening PDFs in a New Window with JavaScript
» Submit Forms Conditionally using JavaScript
» How to Setup a Randomising Function
» Introduction to JavaScript Tutorial

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources