Implement a stack data structure in JavaScript that contains the following operations:
Stack
Creates an instance of a Stack
class that doesn't contain any items. The constructor not accept any arguments.
push
Pushes an item onto the top of the stack.
{*} item
: The item to be pushed onto the stack.{number}
: The new length of the stack.pop
Remove an item at the top of the stack.
{*} item
: The item at the top of the stack if it is not empty, undefined
otherwise.isEmpty
Determines if the stack is empty.
{boolean}
: true
if the stack has no items, false
otherwise.peek
Returns the item at the top of the stack without removing it from the stack.
{*}
: The item at the top of the stack if it is not empty, undefined
otherwise.length
Returns the number of items in the stack.
{number}
: The number of items in the stack.const stack = new Stack();stack.isEmpty(); // truestack.push(1);stack.push(2);stack.length(); // 2stack.push(3);stack.peek(); // 3stack.pop(); // 3stack.isEmpty(); // false