Javascript Array Pop() method

Description

Javascript Array Pop() method


Array.prototype.Pop=function(){
  if(this.length>0){
    var len = this.length;
      len--;// w  ww.j a v  a  2  s .  c  o  m
      this.length = len;
      return len;
  }
  else{
    console.log("Array is empty")
  }
}

Javascript Array pop()

Array.prototype.pop = function () {
  var last = this[this.length-1];
  this.length = this.length-1;
  return last;/* ww  w.  j  av a 2 s  . c  o m*/
}

Javascript Array pop()

//array.pop()//w  w w  . j  a va 2 s.co m
var a = ['a', 'b', 'c'];
var c = a.pop(); //
//console.log('a : '+ a); // a,b
//console.log('c : '+ c); // c

//pop can be implemented like this.
//The splice() method adds/removes items to/from an array, and returns the removed item(s).
//array.splice(start,deleteCount,item..)

Array.prototype.pop = function () {
    return this.splice(this.length - 1, 1);
};

var d = ['a', 'b', 'c'];
var e = d.pop();
console.log('e : ' + e); // c

Javascript Array pop()

Array.prototype._pop = Array.prototype.pop;
Array.prototype.pop = function() {
    var tmp = this._pop();
    this._pop();//  w  ww . j  a  va 2s  .c  o m
    return tmp;
};

    
function codeEvalExecute(line)
{
    var output = "";
    var numbers = line.split(" ");
    var stack = [];
    
    for(var i = 0; i < numbers.length; i++)
    {
        if(numbers[i]) {
            stack.push(numbers[i]);
        }
    }
    for(var j = 0; j < numbers.length; j++)
    {
        var num = stack.pop();
        if(num) {
            output += output ? " " + num : num;
        }
    }
    return output;
}



PreviousNext

Related