0

Ive come to something strange

Im trying to swap the array value 0 with the array value 1

Ive found a previous post stating that this is possible

[arr[0], arr[1]] = [arr[1], arr[0]];

So I wanted to replicate that

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"]
[array_names[0], array_names[1]] = [array_names[1], array_names[0]]

However something strange happens

I get an error message stating that

ReferenceError: Cannot access 'array_names' before initialization

I have checked and there isnt a typo anywhere

Also yes I did initialize the array before doing any modificiation

  • 2
    This is probably due to the lack of `;` to terminate a statement. The second line is combined with the statement of the first line. So `let names = ["a", "b", "c"][names[0], names[1]] ...`, which probably results in the error. Try adding a semicolon `;` at the end of each statement. – 3limin4t0r Feb 18 '23 at 22:09
  • Does this answer your question? [When is it appropriate to use a semicolon?](https://stackoverflow.com/questions/38823062/when-is-it-appropriate-to-use-a-semicolon) – pilchard Feb 18 '23 at 22:50
  • also [Do you recommend using semicolons after every statement in JavaScript?](https://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript) – pilchard Feb 18 '23 at 22:52

3 Answers3

3

Your code is totally correct, but due to a missing semicolon, the interpreter parsed your code differently..

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"]
[array_names[0], array_names[1]] = [array_names[1], array_names[0]]

The expression was evaluated as

["Dave", "johson", ...][array_names[0], array_names[1]] 

The code is parsed as one line statement.

The right answer as stated by Shubhada is

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"]; //Terminate statement
[array_names[0], array_names[1]] = [array_names[1], array_names[0]];
Sadiq Salau
  • 119
  • 5
2

JS is actually trying to interpret your code as :

// reduced for brevity
let array_names = ["Dave", "johson"][array_names[0], array_names[1]] = [array_names[1], array_names[0]]

It's trying to access the index [array_names[0], array_names[1]] of the array ["Dave", "johson"] and at that time, array_names not yet defined.

You can (should) use semicolon to fix that :

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"];
[array_names[0], array_names[1]] = [array_names[1], array_names[0]];
console.log(array_names);
Cid
  • 14,968
  • 4
  • 30
  • 45
1

I try following

let array_names = [
  "Dave", "johson",
  "Lime", "Key",
  "Freeman", "Chell", 
  "Shepart", "Furious"
]

console.log(array_names)

array_names[0], array_names[1] = array_names[1], array_names[0]

console.log(array_names)

temp = array_names[0]
array_names[0] = array_names[1]
array_names[1] = temp

console.log(array_names)

and get the result

[
  'Dave',    'johson',
  'Lime',    'Key',
  'Freeman', 'Chell',
  'Shepart', 'Furious'
]
[
  'Dave',    'johson',
  'Lime',    'Key',
  'Freeman', 'Chell',
  'Shepart', 'Furious'
]
[
  'johson',  'Dave',
  'Lime',    'Key',
  'Freeman', 'Chell',
  'Shepart', 'Furious'
]

as in here. But with your code I obtain

[
  'Dave',    'johson',
  'Lime',    'Key',
  'Freeman', 'Chell',
  'Shepart', 'Furious'
]
[
  'Dave',    'johson',
  'Lime',    'Key',
  'Freeman', 'Chell',
  'Shepart', 'Furious'
]

where the elements are not swapped. I do not understand either.

dudung
  • 499
  • 2
  • 17
  • This answer is missing the `[` and `]` around the two sides of the `=`. `array_names[0], array_names[1] = array_names[1], array_names[0]` is executed as `array_names[0]` followed by `array_names[1] = array_names[1]` followed by `array_names[0]`. Which is not the same as `[array[0], array[1]] = [array[1], array[0]]` which uses [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) to swap the elements. – 3limin4t0r Feb 18 '23 at 22:28