pop() javascript code example
Example 1: Javascript get last item in array
var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
Example 2: js array pop
var array = ['A', 'B', 'C'];
// removes and returns last element
lastElement = array.pop();
Example 3: pop js
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop());
// expected output: "tomato"
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
Example 4: pop array javascript
let array = ["A", "B", "C"];
//Removes the last element of the array
array.pop();
//===========================
console.log(array);
//output =>
//["A", "B"]
//if you want to remove more varibles in the array,
//insted of using push you can simply define the length of the array
let array1 = [1, 2, 3, 4, 5, 6];
//Defines the length of the array to 2, removing the elements
//after the second element
array1.length = 2;
//===========================
console.log(array1);
//output =>
//["1", "2"]
Example 5: javascript pop
var cars = ['mazda', 'honda', 'tesla'];
var telsa=cars.pop(); //cars is now just mazda,honda
Example 6: pop javascript
/*The pop() method removes an element from the end of an array, while shift()
removes an element from the beginning.*/
let greetings = ['whats up?', 'hello', 'see ya!'];
greetings.pop();
// now equals ['whats up?', 'hello']
greetings.shift();
// now equals ['hello']