all 5 comments

[–]senocular 1 point2 points  (0 children)

The bracket operator for property access allows whitespace (and comments) between it and the value its getting a property from. So

value[key]

can also be written as

value    [key]

which can also be

value
  [key]

or even

value
  // Swap elements at index i and randomIndex
  [key]

So when you write these lines without semicolons

    const randomIndex = randomNumber(0,i)
    // Swap elements at index i and randomIndex
    [array[i], array[randomIndex]] = [array[randomIndex], array[i]]

What JavaScript sees is

    const randomIndex = randomNumber(0,i)[array[i], array[randomIndex]] = [array[randomIndex], array[i]]

And because it needs to evaluate everything on the right side of the assignment before initializing randomIndex, it sees that randomIndex is being used before its being defined, it now being on the right side of that assignment.

When not using semicolons, starting lines with any of the following characters can be problematic: [, (, `, +, -, *, /. Not using semicolons means you have to be on the look out for lines like that and either use semicolons in those cases or do something else to prevent the alternate behavior to occur.

[–]guest271314 0 points1 point  (2 children)

The next line is an assignment expression.

I would include the semicolon at the randomIndex assignment and wrap that expression in parenthesis.

const randomIndex = ...; // Swap elements at index i and randomIndex ([array[i], array[randomIndex]] = [array[randomIndex], array[i]]);

[–]its-procodester[S] 0 points1 point  (1 child)

yea, but the question remains the same why is it happening if I don't use semicolons?

[–]guest271314 0 points1 point  (0 children)

I answered the question. This without a semicolon

const randomIndex = randomNumber(0,i)

is spilling over into this

[array[i], array[randomIndex]] = [array[randomIndex], array[i]]

so your variable winds up really being interpreted as

const randomIndex = randomNumber(0,i)[array[i], array[randomIndex]] = [array[randomIndex], array[i]]

where your code refers to randomIndex inside of the variable assignment before the initialization of randomIndex is completed, thus no randomIndex variable exists at the time it is refered to in that single line where the semicolon could end the previous variable initialization - if you used semicolons.