Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rxluz/04233d89610ece86368f98386a1b9908 to your computer and use it in GitHub Desktop.
Save rxluz/04233d89610ece86368f98386a1b9908 to your computer and use it in GitHub Desktop.
JS Data Structures: Arrays, see more at: https://medium.com/p/e1bc57bda950
const animals = ["elephant", "monkey", "snake", "lion"];
const positionToAddNewItem = 2;
for (let index = animals.length; index > positionToAddNewItem; index--) {
animals[index] = animals[index - 1];
}
animals[positionToAddNewItem] = "macaw";
console.log(animals); // will return: [ 'elephant', 'monkey', 'macaw', 'snake', 'lion' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment