Advertisement
benjaminvr

Codecademy - More array methods

Jul 15th, 2021
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains'];
  2.  
  3. // remove the first item using shift
  4. groceryList.shift()
  5. console.log(groceryList);
  6.  
  7. // add 'popcorn' to the front of the array using unshift
  8. groceryList.unshift('popcorn');
  9. console.log(groceryList);
  10.  
  11. // extract a part of the array based on start and end position (non-inclusive)
  12. console.log(groceryList.slice(1,4));
  13.  
  14. // shows that the list is non-mutated after performing a slice operation
  15. console.log(groceryList);
  16.  
  17. // find the index of a particular element using indexOf
  18. const pastaIndex = groceryList.indexOf('pasta');
  19. console.log(pastaIndex);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement