how to pop element from array in javascript code example

Example 1: remove a particular element from array

var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");//get  "car" index
//remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red","blue","green"]

Example 2: remove first element from array javascript

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift();

Example 3: how to remove element from array in javascript

var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");//get  "car" index
//remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red","blue","green"]

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: remove array js

var ar = [1, 2, 3, 4, 5, 6];ar.length = 4; // set length to remove elementsconsole.log( ar ); // [1, 2, 3, 4]
// //////////////////////////////////////
var ar = [1, 2, 3, 4, 5, 6];ar.pop(); // returns 6console.log( ar ); // [1, 2, 3, 4, 5]
// //////////////////////////////////////
var ar = ['zero', 'one', 'two', 'three'];ar.shift(); // returns "zero"console.log( ar ); // ["one", "two", "three"]
// //////////////////////////////////////
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];var removed = arr.splice(2,2);
// //////////////////////////////////////
["bar", "baz", "foo", "qux"]list.splice(0, 2) // Starting at index position 0, remove two elements ["bar", "baz"] and retains ["foo", "qux"].
// //////////////////////////////////////
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
for( var i = 0; i < arr.length; i++){
  if ( arr[i] === 5) {
    arr.splice(i, 1); 
  }
}//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]
// //////////////////////////////////////
var arr = [1, 2, 3, 4, 5, 5, 6, 7, 8, 5, 9, 0];
for( var i = 0; i < arr.length; i++){
  if ( arr[i] === 5) {
    arr.splice(i, 1); i--; 
  }
}//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]
// //////////////////////////////////////
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var filtered = array.filter(function(value, index, arr){
  return value > 5;
});//filtered => [6, 7, 8, 9]//array => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
// //////////////////////////////////////
var evens = _.remove(array, function(n) {
  return n % 2 === 0;
});
console.log(array);// => [1, 3]console.log(evens);// => [2, 4]
// //////////////////////////////////////
function arrayRemove(arr, value) {
  return arr.filter(function(ele){
    return ele != value; });
}
var result = arrayRemove(array, 6);// result = [1, 2, 3, 4, 5, 7, 8, 9, 0]
// //////////////////////////////////////
var ar = [1, 2, 3, 4, 5, 6];delete ar[4]; // delete element with index 4console.log( ar ); // [1, 2, 3, 4, undefined, 6]alert( ar ); // 1,2,3,4,,6

Example 6: javascript array pop

let animals = ["dog","cat","tiger"];

animals.pop(); // ["dog","cat"]

animals.push("elephant"); // ["dog","cat","elephant"]