Javascript裡的陣列已實作Stack(有push, pop等方法),但仍可自己實作Stack
 // 陣列, Stack
 var letters = []; // this is our stack
 // 要確認的單字
 var word = "freeCodeCamp"
 // 儲存反轉後字母的變數
 var rword = "";
 // put letters of word into stack
 // 將字母由前至後依序放入Stack
 for (var i = 0; i < word.length; i++) {
 letters.push(word[i]);
 }
 // pop off the stack in reverse order
 // 將陣列的元素從後開始一一刪除
 for (var i = 0; i < word.length; i++) {
 rword += letters.pop(); 
 // pop執行完畢後回傳被刪除的元素
 }
 if (rword === word) {
 console.log(word + " is a palindrome.");
 }
 else {
 console.log(word + " is not a palindrome.");
 }
var Stack = function() {
 this.count = 0;  // Stack長度
 this.storage = {};  // Stack元素
}
...
 this.push = function(value) {
     this.storage[this.count] = value;
     this.count++;
 }
...
...
 this.pop = function() {
     if (this.count === 0) {
         return undefined;
     }
     this.count--;
     var result = this.storage[this.count];
     delete this.storage[this.count];
     return result;
 }
...
...
 this.size = function() {
   return this.count;
 }
...
...
 this.peek = function() {
     return this.storage[this.count-1];
 }
...
var Stack = function() {
 this.count = 0;  // Stack長度
 this.storage = {};  // Stack元素
    
 // push
 this.push = function(value) {
     this.storage[this.count] = value;
     this.count++;
 }
 // pop
     this.pop = function() {
     if (this.count === 0) {
         return undefined;
     }
     this.count--;
     var result = this.storage[this.count];
     delete this.storage[this.count];
     return result;
 }
    
 this.size = function() {
     return this.count;
 }
 // size
 this.size = function() {
   return this.count;
 }
 // peek
 this.peek = function() {
     return this.storage[this.count-1];
 }
}
  var myStack = new Stack();
  myStack.push(1);
  myStack.push(2);
  console.log(myStack.peek());
  console.log(myStack.pop());
  console.log(myStack.peek());
  myStack.push("freeCodeCamp");
  console.log(myStack.size());
  console.log(myStack.peek());
  console.log(myStack.pop());
  console.log(myStack.peek());
  // 結果
  // "freeCodeCamp is not a palindrome."
  // 2
  // 2
  // 1
  // 2
  // "freeCodeCamp"
  // "freeCodeCamp"
  // 1