114k views
0 votes
How to remove specific value from array in javascript?

User Lashauna
by
7.2k points

1 Answer

6 votes

Final answer:

To remove a specific value from an array in JavaScript, you can use the filter() method. The filter() method creates a new array with all elements that pass a test provided by a callback function.

Step-by-step explanation:

If you want to remove a specific value from an array in JavaScript, you can use the filter() method. The filter() method creates a new array with all elements that pass a test provided by a callback function. In the callback function, you can check if an element is equal to the specific value you want to remove.

let array = [1, 2, 3, 4, 5];

let newArray = array.filter(function(value) {
return value !== 3;
});

console.log(newArray); // Output: [1, 2, 4, 5]
In the example above, the

filter()

method is used to create a new array

newArray

that excludes the value 3. The callback function checks if each value is not equal to 3 using the

!==

operator.

User Pitfall
by
7.8k points