HighDots Forums  

Re: Conditional shortcut (?:) limitations?

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Conditional shortcut (?:) limitations? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael Winter
 
Posts: n/a

Default Re: Conditional shortcut (?:) limitations? - 02-01-2004 , 07:35 PM






On Sun, 1 Feb 2004 17:05:41 -0500, TheKeith <no (AT) spam (DOT) com> wrote:

[snip]

Quote:
Much appreciated--but I'm still confused as to why that is required. If I
can do conditionals in the if...else format without the else statement,
why can't I do the same with ?: operator shortcut?
It's not a shortcut; it's a ternary operator. Unary operators, like !,
require one operand. Binary operators, like +, require two operands.
Ternary operators, like ?:, require three. Only using two operands with
them is like trying to use:

var a = b *; // or a = * b;

Quote:
Also, why does it have to be the word *true*--I tried it with just
putting anything behind the *:*, like *: var hello = "hello"* for
example, and that still causes an error--why is this?
The syntax of the operator is:

(expr) ? (if-true expr) : (if-false expr)

The first expression is evaluated. If it is considered true, the second
operand (if-true expr) is evaluated and returned. If the first expression
is considered false, the third operand (if-false expr) is evaluated and
returned.

The main use for the operator is conditional assignment. Say, for example,
you wanted to ensure that an argument in a function is always valid[1]:

function a( x ) {
x = (undefined == x) ? 0 : x;
}

a();

Here, no argument was passed to function a(), so the expression (undefined
== x) will evaluate to true. This means that the operator, as a whole,
will evaluate to 0 and 0 will be assigned to x.

a( 6 );

Here, (undefined == x) will evaluate as false because x is 6. The
operator, as a whole, will evaluate to 6, so 6 will be assigned to x.

If you are just wanted to do perform something based on a certain
condition, then just use an if statement. If you want an expression to
evaluate to a different value based on a condition, then use the
conditional operator.

I suggest you take a look at a JavaScript reference. Try:

http://devedge.netscape.com/library/...1.5/reference/

Beware of URL wrap.

Mike

[1] This isn't a particularly good example, but it's late and I'm tired,
so bear with me

--
Michael Winter
M.Winter (AT) blueyonder (DOT) co.invalid (replace ".invalid" with ".uk" to reply)


Reply With Quote
  #2  
Old   
TheKeith
 
Posts: n/a

Default Re: Conditional shortcut (?:) limitations? - 02-01-2004 , 08:15 PM







"Michael Winter" <M.Winter (AT) blueyonder (DOT) co.invalid> wrote

Quote:
On Sun, 1 Feb 2004 17:05:41 -0500, TheKeith <no (AT) spam (DOT) com> wrote:

[snip]

Much appreciated--but I'm still confused as to why that is required. If
I
can do conditionals in the if...else format without the else statement,
why can't I do the same with ?: operator shortcut?

It's not a shortcut; it's a ternary operator. Unary operators, like !,
require one operand. Binary operators, like +, require two operands.
Ternary operators, like ?:, require three. Only using two operands with
them is like trying to use:

var a = b *; // or a = * b;

Also, why does it have to be the word *true*--I tried it with just
putting anything behind the *:*, like *: var hello = "hello"* for
example, and that still causes an error--why is this?

The syntax of the operator is:

(expr) ? (if-true expr) : (if-false expr)

The first expression is evaluated. If it is considered true, the second
operand (if-true expr) is evaluated and returned. If the first expression
is considered false, the third operand (if-false expr) is evaluated and
returned.

The main use for the operator is conditional assignment. Say, for example,
you wanted to ensure that an argument in a function is always valid[1]:

function a( x ) {
x = (undefined == x) ? 0 : x;
}

a();

Here, no argument was passed to function a(), so the expression (undefined
== x) will evaluate to true. This means that the operator, as a whole,
will evaluate to 0 and 0 will be assigned to x.

a( 6 );

Here, (undefined == x) will evaluate as false because x is 6. The
operator, as a whole, will evaluate to 6, so 6 will be assigned to x.

If you are just wanted to do perform something based on a certain
condition, then just use an if statement. If you want an expression to
evaluate to a different value based on a condition, then use the
conditional operator.

that cleared it up--thanks a lot.




Reply With Quote
Reply




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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.