Blog Search

Latest Posts

Firefox 3 Web Browser Launched

New Educational Media Website Launch

Trevor Laurency Fitness Website Launch

Recent Recommended Links - April 2008

Web Apps on Your Desktop with Mozilla Prism

Recent Recommended Links - March 2008

Ubuntu Linux - Persistent sudo root

Controlling PHP Register Globals Using .htaccess File

Javascript Essentials - Date Pickers (Calendars)

The new website is here!

Blog Archive

Complete Blog Archive

Browse by Subject

AJAX
Apache
CMS
CSS
HTML
Java
JavaScript
Links
Linux
Mac
MySQL
Pascal
PHP
postgreSQL
RSS
Ruby
SEO
Web Dev News
Windows
XML

Other Resources

Matt's Web Development Blog

Computer Science Student Articles

Affiliate Program Articles

Javascript: Variations of the IF Statement

There are a couple of ways you can use IF statements in JavaScript. One is a more traditional way and the other can be viewed as a shorthand method. The traditional IF statement in JavaScript is as follows:

<script type="text/javascript" language="javascript">
var var1 = 10;
var var2 = 5;
var match = null;
if (var1 == var2) {
  match = 'the variables are the same';
} else {
  match = 'the variables are different';
}
</script>


There is another way we can write IF statements. Take a look at the code below:

<script type="text/javascript" language="javascript">
var var1 = 10;
var var2 = 5;
var match = null;

var1 == var2 ? match = 'same' : match = 'different';
</script>

The fourth line of JavaScript within the tags above represent a whole IF... THEN... ELSE... statement. In fact it is the same statement that is written in the first section of code in this article.

It allows for much more concise code but sacrifices a certain amount of readability. Could certainly come in useful though!

Subscribe to RSS Feed Digg! Add to Google Add to My Yahoo!

Related Links

Related Articles / Posts

Javascript Essentials - Date Pickers (Calendars) (03/03/2008)

Javascript: Variations of the IF Statement (20/09/2006)

Javascript / CSS: Flashing Text - How to do it - Why NOT to do it (16/09/2006)

Code Editors (Syntax Highlighting) - Aptana, Crimson Editor, PHP Designer (14/08/2006)

Web Dev News: Pro Ajax and PHP - Building Highly Interactive Applications (Book) (18/07/2006)

 
Acuras Web Development 21 Hylton Road Sands High Wycombe HP124BZ United Kingdom 08712 887 565
Tel: 08712 887 565
Fax: 08712 887 566
Email: info@acuras.co.uk
Skype: acuras_web
21 Hylton Road
High Wycombe
Buckinghamshire
HP12 4BZ
United Kingdom
Feeds / Content
News
Latest Links
Blog - PHP / CSS