1

I am new to coding in general, and have been working with javascript for awhile now - apologies in advance for any missteps I may have made in posting this question. I researched for the better part of 2 hours and was unable to come up with an answer on my own, so I made an account here.

I came across this answer/code, and am struggling to understand certain aspects of the code. I made it work in my code, but I want to wrap my head around the specific aspects that I don't quite understand.

Specifically, I don't understand how 1) the variable works when it is assigned 3 definitions, with each followed by a comma - which does the variable refer to? Once all of the assignments are defined by the end of the script, how does it know which to use next?

2) I need some clarification on the "switch" portion, and how it works. How does defining each in succession according to the last, encourage the code to switch?

Thank you so much in advance.

E_C
  • 300
  • 2
  • 10

1 Answers1

1

This is simply declaring three variable names, but assigning only currentIndex. temporaryValue and randomIndex are undefined at this point and will be used later:

var currentIndex = array.length, temporaryValue, randomIndex;

This is the same as:

var currentIndex = array.length;
var temporaryValue;
var randomIndex;

One of the difficulties of swapping variables (before JS had nice unpacking/destructuring) is that you couldn't do it in one step.

For example, if you have:

let a = "foo";
let b = "bar";

and you want to swap them. Once you say a = b, you no longer know what a's previous value was because you just overwrote it. The solution is to use a temp variable to save the old a:

let a = "foo";
let b = "bar";

// swap
let temp = a // remember old a value
a = b        // replace a
b = temp     // replace b with the original a

These days you can simply use:

let a = "foo";
let b = "bar";

[a, b] = [b, a]; //Swap in one step

console.log(a, b) 
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Mark
  • 90,562
  • 7
  • 108
  • 148
  • 1
    Thanks so much, Mark - that was really helpful! – E_C Dec 08 '18 at 23:42
  • apologies for the multiple comments, accidentally hit enter - here is the entire comment: Thanks so much, Mark - that was really helpful! So, to clarify, currentIndex is defined only by the leftmost name, array.length - and the variables named after the comma are simply being declared for later use in the function - for simplicity's sake? With the swap, it appears to just use the "old A" or the "temp" by the end. What is the purpose of going through those 3 steps if that is all you are going to use? Thanks again, for the time! – E_C Dec 08 '18 at 23:55
  • Yes, it's three variable declarations separated by commas. One includes an assignment. It's common practice to declare variables at the top of functions. It's not required, but a lot of people do it. I'm not clear what you are asking in the second part. In the shuffle code, the point is to swap two elements of the array so `array[currentIndex] ` and `array[randomIndex]` have swapped places in the array. – Mark Dec 09 '18 at 00:03
  • p.s. some people (including me) dislike the comma-separated declarations. It's very easy to forget a comma which means you end up accidentally declaring globals. – Mark Dec 09 '18 at 00:04
  • 1
    Ahh! Got it! For some reason I was looking at it from a purely numbers standpoint instead of array-position standpoint! Make perfect sense now! Thank you so much! My new-ness is showing haha – E_C Dec 09 '18 at 00:27