Standard: Code doens't work without semicolon

Created on 28 Feb 2020  路  1Comment  路  Source: standard/standard

This is my code:

const colors = ['red', 'green']
[colors[1], colors[0]] = [colors[0], colors[1]]

Without semicolon I recive this error:

ReferenceError: can't access lexical declaration `updatedPreviewBlocks' before initialization

With semicolon, I don't have any error

>All comments

@RodrigoTomeES thank you for using Standard and reporting your issue.

This is one of the few cases where a semicolon is necessary. That's because the first [ of the second line is interpreted as an accessor to the literal array from the first line. Place a semicolon at the start of the second line:

const colors = ['red', 'green']
;[colors[1], colors[0]] = [colors[0], colors[1]]

Read about it here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hzhu picture hzhu  路  3Comments

gkatsanos picture gkatsanos  路  3Comments

rbecheras picture rbecheras  路  3Comments

Alexsey picture Alexsey  路  3Comments

ghost picture ghost  路  3Comments