2

I was going through documentation of JavaScript about arrays and objects.I found one unique query about arrays

var a= [1,2,3]
console.log(a)     // give [1,2,3]


var b = [(1,2) , (3,4) , (5,6)]
console.log(b)     // gives [2,4,6]

I didn't understand the logic behind the same.It just ran over my mind.If we do same thing in python

a = [(1,2),(3,4)]
print(a)     // [(1,2),(3,4)]

Can some one explain the same and can someone explain me how to get the same output in JavaScript like the way I got in python.How to iterate through all the elements in array in javascript

tyler
  • 151
  • 5
  • 12
  • 1
    The result of the expression `(1,2)` in JavaScript is `2`. See [Grouping operator ()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Grouping) and [Comma operator (,)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator) – Phil Sep 02 '20 at 04:27
  • In JavaScript parens don't create a datatype the same way they do in Python. – Alexander Nied Sep 02 '20 at 04:28
  • 1
    I got your point and understood your answer. But now what about the iteration.How can i loop through all the elements inside that array in my case?Is there any way to do it – M K Sep 02 '20 at 04:45
  • 1
    you should not use the grouping operator `()` for such case. javascript !== python. if you code in javascript then think in javascript – Ifaruki Sep 02 '20 at 05:19

2 Answers2

4

I'm not a Python programmer, but those parentheses differ in their functionality between Python and JavaScript.

I understand in Python they indicate a tuple literal. In JavaScript, they merely group things they say "execute each statement and return the last one." That's why you only see some items in your JS arrays.

See also https://stackoverflow.com/a/25280412/1371131

weltschmerz
  • 12,320
  • 9
  • 57
  • 109
2

What you have encountered is the comma operator.

When there is no other context, a , in an expression behaves like an operator just like + or = or / etc.

The comma operator behaves as follows:

  • evaluate the first expression
  • ignore the result of the first expression
  • evaluate the second expression
  • return the value of the second expression

Thus the following code:

a = 1,2;
b = 1,2,3,4;

will result in a being assigned the value 2 and b assigned the value 4.

This may seem kind of silly because we already have ; but the comma operator is useful if you need more than one thing to happen in a single expression context. For example if you need more than one thing to happen in one of the conditions of a for loop you cannot use ; because that is used to separate the conditional expressions. The comma operator comes to the rescue:

for (let a = 0, b = 0;   a < 10;   a++, b++) { //...
//          ^                          ^
//          |                          |____  two things happen here
//          |_______________________________  and here

So (1,2) is just another way to write 2.

Therefore [(1,2) , (3,4) , (5,6)] and [2,4,6] is the same array.

Just like [1+1, 2+2, 3+3] and [2,4,6] is the same array.

They are just different ways to write [2,4,6]. The same way you cannot loop through the array and extract 1+ from [1+1, 2+2, 3+3] you cannot loop through the array and extract (1, from [(1,2) , (3,4) , (5,6)].

If you really need an array of pairs you need to use an array of arrays:

a = [[1,2],[3,4],[5,6]]
slebetman
  • 93,070
  • 18
  • 116
  • 145
  • I got your point and understood your answer. But now what about the iteration.How can i loop through all the elements inside that array in my case.Is there any way to do it – tyler Sep 02 '20 at 04:44
  • You realise right that you only have an array of `[2,4,6]`? Like I said `(1,2)` is just a fancy way to write `2`. There is no data structure as `(1,2)`. So you loop like normal – slebetman Sep 02 '20 at 04:53
  • 1
    If you really want an array of pairs you need to use an array of array: `[[1,2],[3,4],[5,6]]`. There is no way to recover the value `1` from `(1,2)` because it is discarded thus the value of `(1,2)` is `2` – slebetman Sep 02 '20 at 04:56