liveBook · Manning

Pop

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

An array method that removes the last element from an array and returns it.

Need to deal the last card in a deck? Array’s pop method removes and returns the array’s last element. To use it with a Set-based deck, convert the Set to an array first. section 26.1

Listing 26.7. src/cards.js
export function pop(deck) { const card = [ ...deck ].pop(); isCardFlipped.set(card, true); deck.delete(card); return card; }

The pop function spreads the deck into an array, takes its last card, marks it face up, removes it from the Set, and returns it. section 26.1

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Shows the array conversion, last-element removal, card update, Set deletion, and return value.
Examples
Uses a Blackjack deck and a function that returns the last card while removing it from the deck.
What it assumes
Uses JavaScript arrays, Sets, and spread syntax in the card-dealing example.
What you can do after
Apply it via Array.pop to retrieve the last item after converting a Set to an array.
How it connects
Connects Array.pop with Set, spread syntax, and card dealing.

Also explained in

Compare how 4 books explain pop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.