Verifying whether the stack is empty and its size

The count property also works as the size of the stack. So, for the size method, we can simply return the count property:

size() {  return this.count;}

And to verify whether the stack is empty, we can compare if the count value is 0 as follows:

isEmpty() {  return this.count === 0;}

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.