stack in javascript

class Stack{
   constructor() 
    { 
        this.items = []; 
    } 
   
    push(element) 
   { 
    // push element into the items 
    this.items.push(element); 
    }
  
    pop() 
    { 
    if (this.items.length == 0) 
        return "Underflow"; 
    return this.items.pop(); 
    } 
  
    peek() 
	{ 
    	return this.items[this.items.length - 1]; 
	} 
  
    printStack() 
    { 
    	var str = ""; 
    	for (var i = 0; i < this.items.length; i++) 
        	str += this.items[i] + " "; 
    	return str; 
    } 

}

0
5
Tom H 140 points

                                    function Stack() {
  var collection = [];
  this.print = function() {
    console.log(collection);
  };
  this.push = function(val) {
    return collection.push(val);
  };
  this.pop = function() {
    return collection.pop();
  };
  this.peek = function() {
    return collection[collection.length - 1];
  };
  this.isEmpty = function() {
    return collection.length === 0;
  };
  this.clear = function() {
    collection.length = 0;
  };
}

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
creating stack in js stack methods in javascript stack method in javascript implement stack javascript how to declare stack in javascript is there a stack in js make stack javascript javascript how to implement a javascript stack stack explain using javascript stack array js creating a stack in js whait is stack in javascript javascript stack stack implement stack using javascript stacking elements js stack with javascript stack with plain javascript stack javascript array javastript stack js function stack example stack array in javascript Stack js how does the stack work javascript stack arrays in javascript how to create a stack in javascript how to use stack in js javascript .stack implement a stack in js create a stack in js how to create a stack in js making stack javascript stack using js can you stack methods in javascript stack methods javascript stack in java scrip program Stack javascript stack using array js stack in javascript demo to how create stack in javscript what is stack in js what is a stack javascript stack funtions js stack functions js how to use stack in javascript create stack in js stack example in javascript create a stack in javascript stack in js stack implementation javascript stack for js how to make a stack in javascript create a stack javascript how to define a stack in javascript is there stack in javascript all javascript stack what is new stack in javascript stack array javascript implement stack in javascript how to implement a stack javascript stack functions javascript stack in array js create javascript stack stack with js does javascript have stack how to create stack in javascript javscript stack stack with array javascript how to make a stack in javscript what is javascript stack how to implement a stack in javascript creating a stack in javascript what does stack means in javascript? stack implementation in javascript stack javascript example how to implement stack in javascript function stack javascript stack javascript stack js use stack in js javascript stack implementation create stack in javascript stack implementation using javascript what is stack in javascript Stack in JavaScript W3Schools javascript stack stack in js stacking elements in javascript js stack stack in javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source