Clearing the elements of the stack

Finally, we are going to implement the clear method. The clear method simply empties the stack, removing all its elements. The simplest way of implementing this method is as follows:

clear() {  this.items = [];}

An alternative implementation would be calling the pop method until the stack is empty.

And we are done! Our Stack class is implemented.

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.