diff --git a/src/javascript/datastructures/stack/1.stack_with_array/stack_with_array.js b/src/javascript/datastructures/stack/1.stack_with_array/stack_with_array.js index 79ca3c5..95c0b78 100644 --- a/src/javascript/datastructures/stack/1.stack_with_array/stack_with_array.js +++ b/src/javascript/datastructures/stack/1.stack_with_array/stack_with_array.js @@ -21,7 +21,11 @@ class MyStack { } peek() { - return this.array[this.array.length - 1]; // return top most element from the stack without removing the element + // Underflow if stack is empty + if (this.isEmpty()) { + return "Underflow"; + } + return this.array.at(-1); // return top most element from the stack without removing the element } // List of helper functions