Omitting trailing semicolon triggers an error

Spoiler alert - this contains answer to ES6 Challenges, Use Destructuring Assignment to Assign Variables from Arrays.

If I omit the semicolon after b=6:

let a = 8, b = 6 // trailing semicolon omitted
(() => {
  "use strict";
  // change code below this line
  [b,a] = [a,b]
  // change code above this line
})();

I get the following error message:
Uncaught TypeError: 6 is not a function

And if I do this (which also works when the semicolon is present):

let a = 8, b = 6 // trailing semicolon omitted
[b,a] = [a,b]

I get this error message:
Uncaught ReferenceError: Cannot access ‘b’ before initialization

I thought trailing semicolons were optional (except in for loops).

Does anyone know why these errors occur?
:slight_smile:

Because the code reads 6() or 6[] and 6 is not a function nor an object - or something like that
The semicolon works to separate the two things

1 Like

The tests expect a semicolon to be there at the end of the variable assignment line. If you delete stuff outside of the // change code {below|above} this line, the tests will often break.

Hi - I did an independent test outside of FCC Challenge - my second example. ieahleen’s got the answer :slight_smile:

I mean, I am not really sure it is that but it seems plausible enough

This is also why you will sometimes see a semi-colon in front of IIFEs in code to guard against this. Mainly an issue that happens when concatenating files.

;(() => {
  "use strict";
  // change code below this line
  [b,a] = [a,b]
  // change code above this line
})();
1 Like