Javascript Array pop()

Introduction

Javascript array pop() method removes the last element from an array and returns that element.

This method changes the array length .

arr.pop()

Calling pop() on an empty array returns undefined.

Remove the last element of an array:

var languages = ["CSS", "HTML", "Java", "Javascript"];
console.log(languages);//from ww w  . j  av  a 2s  .  c om

languages.pop();
console.log(languages);
let lang = ['CSS', 'HTML', 'Javascript', 'Java'];

let popped = lang.pop();

console.log(lang); //from   w w  w.  j  a  va 2 s .c om
console.log(popped); 

More examples

let colors = new Array(); // create an array 
let count = colors.push("red", "green"); // push two items 
console.log(count); // 2 

count = colors.push("black"); // push another item on 
console.log(count); // 3 

let item = colors.pop(); // get the last item 
console.log(item); // "black" 
console.log(colors.length); // 2 



PreviousNext

Related