Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The result of Array Matching is wrong #130

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open

The result of Array Matching is wrong #130

wants to merge 1 commit into from

Conversation

jasonkim7288
Copy link

The result from ES6 Array Matching is not what it is expected to be with syntactic sugar which is the default.

var list = [ 1, 2, 3 ]
var [ a, , b ] = list
[ b, a ] = [ a, b ]

The result is supposed to be a is 3 and b is 1, but due to the syntactic sugar, a and b are all undefined.
That is because the code will be like

var [ a, , b ] = list[ b, a ] = [ a, b ]

Therefore, I just put square brackets for the last sentence.

@kamil-kielczewski
Copy link

kamil-kielczewski commented Nov 1, 2022

@jasonkim7288 Good question - but this is not SYNTAX SUGAR. Your example shows why you should allways use semiclon ; at the end of the line (because it is easy to miss place where JS join lines). Below code works as expected

var list = [ 1, 2, 3 ];
var [ a, , b ] = list;
[ b, a ] = [ a, b ];

@aziyatali
Copy link

If we are so obsessed with writing code in one line using spread operators:
var [b, ,a] = [a, ,b] = list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants