Skip to content

Instantly share code, notes, and snippets.

@darleykrefta
Created March 20, 2021 13:19
Show Gist options
  • Save darleykrefta/b0e8fbe6867ba7ed818b8a312ab31d73 to your computer and use it in GitHub Desktop.
Save darleykrefta/b0e8fbe6867ba7ed818b8a312ab31d73 to your computer and use it in GitHub Desktop.
const jedis = ['Yoda', 'Luke Skywalker', 'Qui-Gon Jinn', 'Rey']

// define jedisSorted with a spread of jedis, creating a new sorted reference
const jedisSorted = [...jedis].sort()

// the original variable wasn't updated
console.log(jedis)
// ['Luke Skywalker', 'Qui-Gon Jinn', 'Rey', 'Yoda']

// only the new one was updated
console.log(jedisSorted)
// ['Luke Skywalker', 'Qui-Gon Jinn', 'Rey', 'Yoda']

// define jedisReversed with a spread of jedis, creating a new reversed reference
const jedisReversed = [...jedis].reverse()

// the original variable wasn't updated
console.log(jedis)
// ['Yoda', 'Rey', 'Qui-Gon Jinn', 'Luke Skywalker']

// only the new one was updated
console.log(jedisReversed)
// ['Yoda', 'Rey', 'Qui-Gon Jinn', 'Luke Skywalker']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment