remove array value in javascript code example

Example 1: remove a value from array js

const index = array.indexOf(item);
if (index > -1) {
	array.splice(index, 1);
}

Example 2: delete from array based on value javascript

var index = array.indexOf(item);
if (index !== -1) array.splice(index, 1);

Example 3: how to remove item from array javascript

var array = ["Item", "Item", "Delete me!", "Item"]

array.splice(2,1) // array is now ["Item","Item","Item"]

Example 4: remove elements from array js

const array = [2, 5, 9];

console.log(array);

const index = array.indexOf(5);
if (index > -1) {
  array.splice(index, 1);
}

// array = [2, 9]
console.log(array);

Example 5: remove a value to an array of javascript

var data = [1, 2, 3];

// remove a specific value
// splice(starting index, how many values to remove);
data.splice(1, 1);
// data = [1, 3];

// remove last element
data.pop();
// data = [1, 2];

Example 6: remove element from array javascript

let numbers = [1,2,3,4]

// to remove last element
let last_num = numbers.pop();
// numbers will now be [1,2,3]

// to remove first element
let first_num = numbers.shift();
//numbers will now be [2,3]

// to remove at an index
let number_index = 1
let index_num = numbers.splice(number_index,1); //removes 1 element at index 1
//numbers will now be [2]

//these methods will modify the numbers array and return the removed element