Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active April 8, 2020 03:20
Show Gist options
  • Save sandrabosk/782f727265c91b06bf51f0bb565a4baf to your computer and use it in GitHub Desktop.
Save sandrabosk/782f727265c91b06bf51f0bb565a4baf to your computer and use it in GitHub Desktop.

.reverse()

  • mutates the original array so needs to be cloned before applying .reverse() on it
  • returns the array of the same size in reversed order

Source

We will be working on the following array:

const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

Let's first copy/clone array and then apply the .reverse() on it:

console.log(`original: ${nums}`); original: 0,1,2,3,4,5,6,7,8,9

// .slice() makes a copy
const reversed = nums.slice().reverse(); 

console.log(`reversed: ${reversed}`); // reversed: 9,8,7,6,5,4,3,2,1,0

console.log(`original: ${nums}`); original: 0,1,2,3,4,5,6,7,8,9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment