Java script : More Array Methods

More Array Methods

There are many more array methods than just .push() and .pop(). You can read about all of the array methods that exist on the Mozilla Developer Network (MDN) documentation.
.pop() and .push() modify the array on which they're called. However, there are some array methods that don't modify the array. Be sure to check MDN to understand the behavior of the method you are using.
Some methods that JavaScript developers use frequently are .join().slice().splice().shift().unshift(), and .concat() amongst many others.
Below, we will explore some methods that we have not learned yet. We will use these methods to edit a grocery list. As you complete the steps, you can consult the MDN documentation to learn what each method does!
1.
Use the .shift() method to remove the first item from the array groceryList.
Log the new groceryList to the console.
Stuck? Get a hint
2.
Use the .unshift() method to add 'popcorn' to the beginning of your grocery list.
Log the new grocery list to the console.
You may wish to delete the console.log() statement from the previous step.
Stuck? Get a hint
3.
You're in a hurry so you decide to ask a friend to help you with your grocery shopping. You want him to pick up the bananas, coffee beans, and brown rice.
Use .slice() to provide him with a list of these three things.
Log this part of the list to the console. Unlike the two previous checkpoints, you should do both of these steps in one line.
Stuck? Get a hint
4.
Log the grocery list to the console one more time.
Notice that the groceryList array still contains the same items it had in Step 2.

Tidak ada komentar:

Posting Komentar