473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

shorthand syntax reference?

Usually I write something like ...

if (x==1) { doSomething() }

.... and that's how I see it in all the JS references I've found so
far.

But I have seen shorthand (?) for the same thing (?), like this ...

(x==1) ? doSomething()

.... and several other "alternate" methods.

Are they documented somewhere? I kinda like them, but are they
supported in the usual gang of browsers? or is it stupid to use them
because they are fringe/sloppy?

Thnx in advance :)

Apr 15 '07 #1
10 2674
töff said the following on 4/14/2007 11:06 PM:
Usually I write something like ...

if (x==1) { doSomething() }

... and that's how I see it in all the JS references I've found so
far.

But I have seen shorthand (?) for the same thing (?), like this ...

(x==1) ? doSomething()

... and several other "alternate" methods.
That is commonly called a "ternary expression" and your example is
missing the third part of it.

condition?true:false

Is the construct. If the condition is true then the true part gets
executed, if it is false then the false part gets executed.
Are they documented somewhere? I kinda like them, but are they
supported in the usual gang of browsers? or is it stupid to use them
because they are fringe/sloppy?
There is nothing stupid about the ternary operator.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 15 '07 #2
On Apr 14, 9:33 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
condition?true:false

Is the construct. If the condition is true then the true part gets
executed, if it is false then the false part gets executed.

condition?expression1:expression2

The operator _returns_ expression1 if the condition is true,
otherwise it returns expression2.
Are they documented somewhere?
http://msdn2.microsoft.com/en-us/library/be21c7hw.aspx

Apr 15 '07 #3
scripts.contact wrote on 15 apr 2007 in comp.lang.javascript:
On Apr 14, 9:33 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
>condition?true:false

Is the construct. If the condition is true then the true part gets
executed, if it is false then the false part gets executed.


condition?expression1:expression2

The operator _returns_ expression1 if the condition is true,
otherwise it returns expression2.
True, but disregarding the use of,
or in absense of a result value,
it can be and often is used
simply to execute one of those expressions:
(a>27.3) ? alert('high') : alert('low');
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 15 '07 #4
"Evertjan." schreef
simply to execute one of those expressions:

(a>27.3) ? alert('high') : alert('low');
Simpler still, in this case, unless one of those
statements/expressions/values is likely to change from an alert to a confirm
or prompt or anything else, is of course:

alert( a>27.3 ? 'high' : 'low' );

hth
Yorick
http://www.yorick.onlyfools.com/
Apr 15 '07 #5
Yorick wrote on 15 apr 2007 in comp.lang.javascript:
"Evertjan." schreef
>simply to execute one of those expressions:

(a>27.3) ? alert('high') : alert('low');

Simpler still, in this case, unless one of those
statements/expressions/values is likely to change from an alert to a
confirm or prompt or anything else, is of course:

alert( a>27.3 ? 'high' : 'low' );
Sure, but that was not the point.

The point was to show a tertiary choosing between functions without result.

Jammer dan.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 15 '07 #6
Evertjan. scribed:
>Yorick wrote on 15 apr 2007 in comp.lang.javascript:
>"Evertjan." schreef
>>simply to execute one of those expressions:

(a>27.3) ? alert('high') : alert('low');

Simpler still, in this case, unless one of those
statements/expressions/values is likely to change from an alert to a
confirm or prompt or anything else, is of course:

alert( a>27.3 ? 'high' : 'low' );

Sure, but that was not the point.

The point was to show a tertiary choosing between functions without result.

Jammer dan.
Is there a level of nested instructions, independent of easing code review,
that is recommended against? In your example above, 27.3 is the result of a
complex calculation, etc. Aside from reading ease, is there any reason one
should avoid lengthy, one-line, all-inclusive instructions, e.g., including
all the code that resulted in 27.3?
--
Ed Jay (remove 'M' to respond by email)
Apr 15 '07 #7
Ed Jay wrote on 15 apr 2007 in comp.lang.javascript:
>>alert( a>27.3 ? 'high' : 'low' );

Sure, but that was not the point.

The point was to show a tertiary choosing between functions without
result.

Jammer dan.

Is there a level of nested instructions, independent of easing code
review, that is recommended against?
In general there is not, methinks, up to the amount of memory the
instruction stack takes or the time the instruction sequence takes before a
implemented timeout. Also a integer counter in your code, needed for
handling such deep nesting, or result variable, could overflow.

Try a reentrant faculty(n) code.

Specifically, as it is javascript we are discussing and not a specific
interperting engine, there could be implementation dependent limitations.
In your example above, 27.3 is
the result of a complex calculation, etc. Aside from reading ease, is
there any reason one should avoid lengthy, one-line, all-inclusive
instructions, e.g., including all the code that resulted in 27.3?
Next to reading ease, also debugging and error line reporing ease,
methinks.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 15 '07 #8
Evertjan. scribed:
>Ed Jay wrote on 15 apr 2007 in comp.lang.javascript:
>>>alert( a>27.3 ? 'high' : 'low' );

Sure, but that was not the point.

The point was to show a tertiary choosing between functions without
result.

Jammer dan.

Is there a level of nested instructions, independent of easing code
review, that is recommended against?

In general there is not, methinks, up to the amount of memory the
instruction stack takes or the time the instruction sequence takes before a
implemented timeout. Also a integer counter in your code, needed for
handling such deep nesting, or result variable, could overflow.

Try a reentrant faculty(n) code.

Specifically, as it is javascript we are discussing and not a specific
interperting engine, there could be implementation dependent limitations.
>In your example above, 27.3 is
the result of a complex calculation, etc. Aside from reading ease, is
there any reason one should avoid lengthy, one-line, all-inclusive
instructions, e.g., including all the code that resulted in 27.3?

Next to reading ease, also debugging and error line reporing ease,
methinks.
Thanks.
--
Ed Jay (remove 'M' to respond by email)
Apr 15 '07 #9
In comp.lang.javascript message <11**********************@n76g2000hsh.go
oglegroups.com>, Sun, 15 Apr 2007 03:13:04, scripts.contact
<sc*************@gmail.composted:
>On Apr 14, 9:33 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
>condition?true:false

Is the construct. If the condition is true then the true part gets
executed, if it is false then the false part gets executed.


condition?expression1:expression2

The operator _returns_ expression1 if the condition is true,
otherwise it returns expression2.
Are they documented somewhere?

http://msdn2.microsoft.com/en-us/library/be21c7hw.aspx
That document describes Jscript, and includes "This page is specific to
..NET Framework 3.0".

That site is not therefore a reliable guide for those writing javascript
for the Web; for that, you want a site describing standards which all
browsers can be expected to aspire to agreeing with.

But in this case it is not misleading.

--
(c) John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Apr 15 '07 #10
In comp.lang.javascript message <11**********************@o5g2000hsb.goo
glegroups.com>, Sat, 14 Apr 2007 20:06:37, töff <to**@arcticcoconut.com>
posted:
>
But I have seen shorthand (?) for the same thing (?), like this ...

(x==1) ? doSomething()

... and several other "alternate" methods.

Are they documented somewhere? I kinda like them, but are they
supported in the usual gang of browsers? or is it stupid to use them
because they are fringe/sloppy?
For such questions, you should look in ISO-16262, which should be linked
from the FAQ; failing that, try ECMA-262.

Such standards rarely contain the sentence-terminating question mark of
doubt as such, so a search for the character will be easy enough.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 15 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
4
by: Toonman | last post by:
I'm trying to use a couple of variables in a stored procedure. Things work fine when I hard code the data into the variables and also work fine when I use the variable in the WHERE clause and hard...
7
by: Fabian Neumann | last post by:
Hi! I got a problem with font-family inheritance. Let's say I have CSS definitions like: p { font:normal 10pt Verdana; } strong { font:normal 14pt inherit;
7
by: Dave | last post by:
Occasionally, I've seen people write CSS code in shorthand for specifying font styles. I don't exactly remember how it goes, but sometimes I see things like this: SPAN {font: arial, helvetica...
9
by: Andy | last post by:
I'm a beginning student of CSS and the shorthand order for margins, etc. raised an eyebrow. The clockwise order is what bothers me: p.margin {margin: cm cm cm cm} I think the order should...
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
13
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format...
7
by: castironpi | last post by:
any( iterab ) and all( iterab ) as shorthand for reduce( operator.or_, iterab ) and reduce( operator.and_, iterab ). What do you think?
5
Banfa
by: Banfa | last post by:
So I have a little problem, I have a template class and that class contains a template function; now what I want to do is declare that function in the class (or indeed the entire class) as a friend...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.