Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-30-2007, 03:33 PM
Scorpian8867's Avatar
Junior Member
 
Join Date: Feb 2007
Location: Texas
Posts: 24
Scorpian8867 is on a distinguished road
Send a message via AIM to Scorpian8867 Send a message via MSN to Scorpian8867
[JS] Ternary Operators (One-Line Conditionals)

Alright, now in JavaScript, there are several ways to execute certain commands under certain circumstances. There's the classic if/else statement:

if(condtion){
  code to be executed if true
}else{
  code to be executed elsewise
}


There's also the switch/case function:

switch(variable){
  case "value": code to be executed break;
  case "value2": code to be executed break;
  default: code to be executed if neither of above break;
}


However, what some people don't seem to realize is the use of one-line conditionals. But wait, what are "one-line conditionals"? Well, they're an if/else statement that's used to store a value on "one-line", so to say. For example, let's say that you are trying to calculate whether the value of the variable vamp is 20 or 30. In order to do this, we need to know the vaule of trip, which is 10 more then vamp needs to be. How could we do this? Well, we could use an if/else statement, like this:

if(trip == 30){
  var vamp = 20;
}else{
  var vamp = 30;
}


That's one way to do it, but that seems kind of long, doesn't it? Let's use a one-line conditional instead:

var vamp = (trip == 30) ? 20 : 30;

Way shorter, isn't it? So, how did we do it? Here's what a one-line conditional looks like:

variable = (condition) ? what to store if true : what to store if not true;

Remember, you can also use this to store different vaules for object properties. For example:

theTable.style.display = (theTable.style.display == 'none') ? 'block' : 'none';

This can also be used in another way, don't forget.

var table = theTable[(x == 0 ? 1 : 0)];

That layout is slightly different from the one I showed you. It's the same, but you move the closing ) to after the "else" value, and before the ending semicolon. This is used to find variables in one small chunk right in the middle of another code, eliminating the need for an extra variable to store the vaule in.

In conclusion, the use of one-line conditionals can be used to seriously shorten then length of a code. It can get rid of unnecessary variables and loading time that will slow the user down. Overall, it's pretty useful.

__________________
RoddyInnovations.com
Reply With Quote
  #2 (permalink)  
Old 04-04-2007, 06:47 PM
Junior Member
 
Join Date: Mar 2007
Posts: 20
kraze101 is on a distinguished road
i know that this question is off topic alittle but does this operator used by other languanges too for example php and stufF??

thanks for the tut i think im getting this alittle clearly.Hopefully i find a way to use it
Reply With Quote
  #3 (permalink)  
Old 04-04-2007, 11:25 PM
Scorpian8867's Avatar
Junior Member
 
Join Date: Feb 2007
Location: Texas
Posts: 24
Scorpian8867 is on a distinguished road
Send a message via AIM to Scorpian8867 Send a message via MSN to Scorpian8867
I know PHP supports it as well. I think Perl/CGI also supports it, but I'm not sure. PHP is the one language besides JS I can confirm that on.

__________________
RoddyInnovations.com
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 01:44 AM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com