Home>
push () method

1. Definition:Add one or more elements to the end of the array,And return the new length.

2. Syntax:arr.push (element1, ..., elementn)

3. Parameters:can receive any number of parameters

4. Return value:Returns the length of the modified array.

var arr1=[1, 2, 3, 4];
var arr2=["c", "b", "a"];
array.prototype.copypush=function () {
  for (var i=0;i<arguments.length;i ++) {
    this [this.length]=arguments [i];
  }
  return this.length;
};
console.log (arr1.push ("a", "b"));//6
console.log (arr1);//[1, 2, 3, 4, "a", "b"]
console.log (arr2.push ());//3
console.log (arr2);//["c", "b", "a"]

operation result:

pop () method

1. Definition:remove the last item from the end of the array,Reduce the length of the array and return the removed items.

2. Syntax:arr.pop ()

3. Parameters:/

4. Return value:the element removed from the array (undefined when the array is empty).

var arr1=[1, 2, 3, 4];
var arr2=[];
array.prototype.copypop=function () {
  var result=null;
  if (this.length == 0) {//returns undefined when the array is empty
    return undefined;
  }
  result=this [this.length-1];
  this.length=this.length-1;
  return result;
};
console.log (arr1.copypop ());//4
console.log (arr1);//[1, 2, 3]
console.log (arr1.length);//3
//When the array is empty
console.log (arr2.length);//0
console.log (arr2.copypop ());//undefined
console.log (arr2);//[]
console.log (arr2.length);//0

operation result:

  • Previous C # implementation of binary conversion (binary, hexadecimal, decimal conversion)
  • Next PHP pthread extension use and attention