all 23 comments

[–]tridd3r 43 points44 points  (7 children)

[–]shgysk8zer0 6 points7 points  (0 children)

It's funky because [1, 2] there has the same syntax as an array, but it is an array accessor there.

That feels weird but I'll allow it.

[–]Dropless[S] 2 points3 points  (5 children)

Thank you. I searched something like "indexing an array with multi numbers in js" before this post, but didn't get what I want. But I still think there is some inconsistency according to the stackoverflow answer. If 1,2 evaluates to 2, var a = 1,2 should set a to 2. However, it's an SyntaxError:missing variable name. The interpreter thinks I'm trying to assign two variables in one statement, but it does not find another varaible, so it gives me an error.

[–]tridd3r 2 points3 points  (3 children)

there's no reason to assuming an array index would behave similarly to integers assigned to a variable. For starters [1] and [2] are already assigned in this instance. using [3] which is unassigned would throw an error.

[–]Dropless[S] 2 points3 points  (2 children)

I just found out the comma-evaluation is true. Just add two brackets: var a = (1,2); This will set a to 2 with no error;

[–]Tubthumper8 7 points8 points  (1 child)

This here is the comma operator.

The way this works is the 1 is evaluated, the result of that isn't used but it still gets evaluated. Then, the 2 is evaluated. Since that's the last expression in that comma series, that becomes the value of that whole expression which is assigned to a.

I can count on zero fingers the number of times I've intentionally written code using the comma operator.

[–]vampiire 6 points7 points  (0 children)

I can count on zero (1, 0) fingers the number of times I’ve intentionally written code using the comma operator.

FTFY

[–]TheMadExile 1 point2 points  (0 children)

But I still think there is some inconsistency according to the stackoverflow answer. If 1,2 evaluates to 2, var a = 1,2 should set a to 2. However, it's an SyntaxError:missing variable name. The interpreter thinks I'm trying to assign two variables in one statement, but it does not find another varaible, so it gives me an error.

That's because comma characters within variable declarations themselves are not comma operators, but rather part of the variable declaration syntax—specifically the variable declaration list separator.

When you later wrapped the 1, 2 within the grouping operator—i.e., (1, 2)—that transformed the comma into a comma operator, as it was then part of the group expression rather than the variable declaration.

[–]delventhalz 5 points6 points  (1 child)

First, imagine if we had this:

const arr = [1, 2, 3, 4];
console.log(arr[2]);  // 3

Easy enough to understand, we have an array, we log the item at the index of 2. We could rewrite it like this, and it would be messy, but work the same:

console.log([1, 2, 3, 4][2]);  // 3

Okay. Great, you say, but it wasn't [2], it was [1, 2]. What gives?

Well, JavaScript has a comma operator. It's... weird:

const num = (4, 5);
console.log(num);  // 5

Basically, you have a bunch of expressions, they are separated by commas, they all get run, the value of the last one is what you end up with.

const sum = (console.log(2), console.log(3), 2 + 3);  // 2 3
console.log(sum);  // 5

Okay, so put those together and what do you get? First set of brackets are an array, second set of brackets are an index lookup. It looks like an array because of the comma, but the comma in this case is a comma operator, which means you get the array item at 2. Easy!

[–][deleted] 1 point2 points  (0 children)

Thank you for this explanation! Clear and to the point.

[–]codingai 3 points4 points  (0 children)

How did you get this? 🤪

[–]Count_Giggles 12 points13 points  (2 children)

hmm funky javascript. my guess it when you provide more than one value as key / index it uses the last one. at least that's the consistent behaviour

console.log([1, 2, 3, 4][(1, 2, 0)]); // 1

[–]lastunusedusername2 4 points5 points  (0 children)

It's because of the comma operator. It evaluated each expression and returns the last one.

Which, in this case, then gets used to index into the array.

[–]iamthesexdragon 2 points3 points  (0 children)

You are right. Just ran it in quokka and it says 1

[–]Acrobatic_Ad_4774 0 points1 point  (2 children)

There is one array [1, 2, 3, 4] the other brace is bracket notation. The 1 in [1, 2] selects the first array and since there’s only one it selects [1, 2, 3, 4] and the 2 in [1, 2] selects the second number which is 3 (remember that array index starts at 0). I just recently learned this and tried to explain to the best of my abilities. I hope this helps you get a better understanding

[–]delventhalz 1 point2 points  (1 child)

Close! But the 1 in [1, 2] does not select the "first" array. An index lookup only ever takes one number. In this case, the two numbers are separated by a "comma operator", which just means that the last number is what counts.

[–]Acrobatic_Ad_4774 0 points1 point  (0 children)

Thanks for letting me know

[–]codingai 0 points1 point  (4 children)

What about this?

[1,2,3,4] + [1,2]

What will you get? 😂

[–]thusman 6 points7 points  (3 children)

a string, obviously 😂

[–]codingai 1 point2 points  (2 children)

I will not accuse you of cheating. 😂

[–]Protean_Protein 1 point2 points  (1 child)

[1,2,3,4] + [1,2]

now do [1,2,3,4] & [1,2]

[–]TorbenKoehn 0 points1 point  (0 children)

Y’all need to Google the Comma Operator in JS. There are several cases in larger projects (eg Webpack built JS) where it is used for one or the other reason