Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22451 > unrolled thread

How to understand return a,b statement ?

Started byGuillaume Biton <guillaume.biton@gmail.com>
First post2013-12-28 08:57 -0800
Last post2013-12-29 18:22 +0000
Articles 7 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  How to understand return a,b statement ? Guillaume Biton <guillaume.biton@gmail.com> - 2013-12-28 08:57 -0800
    Re: How to understand return a,b statement ? Guillaume Biton <guillaume.biton@gmail.com> - 2013-12-28 09:03 -0800
      Re: How to understand return a,b statement ? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-28 16:00 -0200
        Re: How to understand return a,b statement ? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-28 20:54 +0000
          Re: How to understand return a,b statement ? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-28 23:16 -0200
          Re: How to understand return a,b statement ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-29 17:25 +0100
          Re: How to understand return a,b statement ? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-29 18:22 +0000

#22451 — How to understand return a,b statement ?

FromGuillaume Biton <guillaume.biton@gmail.com>
Date2013-12-28 08:57 -0800
SubjectHow to understand return a,b statement ?
Message-ID<ec287c43-ac92-489e-8140-55272ace07bb@googlegroups.com>
Hello,

I have difficulties to understand the statement :

return a, b

When I try I always get b value ? Anyone can explain ?

Regards

[toc] | [next] | [standalone]


#22452

FromGuillaume Biton <guillaume.biton@gmail.com>
Date2013-12-28 09:03 -0800
Message-ID<1368da15-53d4-457c-9ee1-3e9f90b677d2@googlegroups.com>
In reply to#22451
Ok I found my reply here : https://developer.mozilla.org/fr/docs/JavaScript/R%C3%A9f%C3%A9rence_JavaScript#Opérateurs

Comma Operator
(,)

The comma operator allows multiple expressions to be evaluated in a single statement and returns the result of the last expression.

Sorry for the question

Le samedi 28 décembre 2013 17:57:59 UTC+1, Guillaume Biton a écrit :
> Hello,
> 
> 
> 
> I have difficulties to understand the statement :
> 
> 
> 
> return a, b
> 
> 
> 
> When I try I always get b value ? Anyone can explain ?
> 
> 
> 
> Regards

[toc] | [prev] | [next] | [standalone]


#22453

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2013-12-28 16:00 -0200
Message-ID<l9n3ka$d47$1@speranza.aioe.org>
In reply to#22452
On 28/12/2013 15:03, Guillaume Biton wrote:
> Ok I found my reply here : https://developer.mozilla.org/fr/docs/JavaScript/R%C3%A9f%C3%A9rence_JavaScript#Opérateurs
>
> Comma Operator
> (,)
>
> The comma operator allows multiple expressions to be evaluated in a single statement and returns the result of the last expression.
>
> Sorry for the question
>
> Le samedi 28 décembre 2013 17:57:59 UTC+1, Guillaume Biton a écrit :
>> Hello,
>>
>>
>>
>> I have difficulties to understand the statement :
>>
>>
>>
>> return a, b
>>
>>
>>
>> When I try I always get b value ? Anyone can explain ?

According to Douglas Crockford's book (JavaScript: The Good Parts), the 
comma operator should be used only inside for statements, because it can 
lead to excessively tricky expressions or mask programming errors. 
Thereby JSLint will complain if it finds the comma operator used outside 
the initialization and incrementation parts of the for statement.

However, there are cases in which the comma operator might be useful, 
although the code would become cryptic to other developers who weren't 
familiarised with it.

Check this out:

<http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/>


-- 
Joao Rodrigues

[toc] | [prev] | [next] | [standalone]


#22457

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-12-28 20:54 +0000
Message-ID<l9ndpk$d2v$1@dont-email.me>
In reply to#22453
On Sat, 28 Dec 2013 16:00:46 -0200, Joao Rodrigues wrote:

> According to Douglas Crockford's book (JavaScript: The Good Parts), the
> comma operator should be used only inside for statements

If I might suggest another use of the comma:

var a=2, b=6, c="fred", d={'time':0,amount:'0','items':[]};

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#22464

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2013-12-28 23:16 -0200
Message-ID<l9nt4h$6m3$1@speranza.aioe.org>
In reply to#22457
On 28/12/2013 18:54, Denis McMahon wrote:
> On Sat, 28 Dec 2013 16:00:46 -0200, Joao Rodrigues wrote:
>
>> According to Douglas Crockford's book (JavaScript: The Good Parts), the
>> comma operator should be used only inside for statements
>
> If I might suggest another use of the comma:
>
> var a=2, b=6, c="fred", d={'time':0,amount:'0','items':[]};

Well, these commas are mere separators in the variable statement (ES 5.1 
§ 12.2). The comma is also used to separate elements of Arrays and 
Objects. But the comma operator (ES 5.1 § 11.14) is something else.

E.g of comma separator:

var colorIndex = 0,
     colors = ["FF0000", "008000", "FF0086", "A2FF00",
               "0000FF", "800080"];

E.g of the comma operator:

function selectNextColor() {
   return colors[colorIndex++] || colors[colorIndex = 0, colorIndex++];
}


JSLint expects to find the comma operator only inside for loops:

for (var i = 0, len = arr.length; i < len; i++) {...}

But we can use the comma operator in other parts of our code:

var el, len = elems.length - 1;
while (el = elems[len], len--) {
   if (el.type === 'checkbox') {
     //...
   }
}

var s = '{"foo": "bar"}';
var o = eval("1," + s);

Or:

var s = "foo";
1, {
      "foo": ["baz", "bar"]
    }[s].sort();

-- 
Joao Rodrigues

[toc] | [prev] | [next] | [standalone]


#22474

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-12-29 17:25 +0100
Message-ID<1596269.Jb2yfOpZAA@PointedEars.de>
In reply to#22457
Denis McMahon wrote:

> On Sat, 28 Dec 2013 16:00:46 -0200, Joao Rodrigues wrote:
>> According to Douglas Crockford's book (JavaScript: The Good Parts), the
>> comma operator should be used only inside for statements
> 
> If I might suggest another use of the comma:
> 
> var a=2, b=6, c="fred", d={'time':0,amount:'0','items':[]};

Those are commas, but not comma operators.
 
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#22487

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-12-29 18:22 +0000
Message-ID<BimGjTXphGwSFwxl@invalid.uk.co.demon.merlyn.invalid>
In reply to#22457
In comp.lang.javascript message <l9ndpk$d2v$1@dont-email.me>, Sat, 28
Dec 2013 20:54:12, Denis McMahon <denismfmcmahon@gmail.com> posted:

>On Sat, 28 Dec 2013 16:00:46 -0200, Joao Rodrigues wrote:
>
>> According to Douglas Crockford's book (JavaScript: The Good Parts), the
>> comma operator should be used only inside for statements
>
>If I might suggest another use of the comma:
>
>var a=2, b=6, c="fred", d={'time':0,amount:'0','items':[]};


or even (tested only in Firefox 26.0) :

var i = "aye", y = i + "-" + i,  i = "hush", z = i + "-" + i
var a = "a", a = a + a, a = a + a, a = a + a, a = a + a, a = a + a

-- 
 (c) John Stockton, nr London, UK.  For Mail, see Home Page.  Turnpike, WinXP.
 Web  <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
 Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
 DOS/Win/UNIX now 2.0.6;  see my <http://www.merlyn.demon.co.uk/pc-links.htm>.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web