Skip to content

Instantly share code, notes, and snippets.

@nydame
Last active May 20, 2018 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nydame/c5bd99db9c87097724feec7cd5af33c1 to your computer and use it in GitHub Desktop.
Save nydame/c5bd99db9c87097724feec7cd5af33c1 to your computer and use it in GitHub Desktop.
Convert a 1-dimensional array to a parameter list (JavaScript)

#Array-to-parameter-list conversion

##During a recent mock technical interview, I was asked to find the largest of a series of numbers stored in an array. The first solution I hit upon was to feed all the array members to Math.max(), but I just couldn't figure out how to do it, and I ended up taking another approach. When the interview was over, the interviewer gave me two ways I could've made my original idea work.

###(1) Use apply()

const numberArray = [12, 45, 0, 7];
return Math.max.apply( null, numberArray ); // 45

###(2) Use the new spread operator

const numberArray = [12, 45, 0, 7];
return Math.max( ...numberArray ); // 45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment