Assign variables from Arrays

Please check my code below.

const [x,,,y]  = [1,2,3,4,5];
console.log(x,y);

what will it should return ?
I check it returns 1 4

But shouldn’t it have to return 1 5

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Count your commas carefully.

Yes I’ve count it there are 3 commas means.

const [x , , , y] = [1,2,3,4,5];
console.log(x,y);

which means index 0 1 2 3 4 = 1 , , , 5

How many commas are there in [1,2,3,4,5]?

How many commas are there in [1,2,3,4,5] ?

4

And how many commas are there in [x,,,y]?

And how many commas are there in [x,,,y] ?

3

That’s why you are assigning 4 to y instead of 5.

You can also look at it visually to see how they “line up”:

[1,2,3,4,5]
[x, , ,y]