Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Removing Negatives from Array in JavaScript
In JavaScript, you often need to remove negative values from an array. This article shows three effective methods to accomplish this task.
Input-Output Scenarios
Let's look at typical scenarios. When an array contains both negative and positive values, we need to filter out the negatives:
Input = [-2, 5, -7, 32, 78, -32] Output = [5, 32, 78]
If the array contains only positive values, it remains unchanged:
Input = [56, 43, 12, 67, 69, 34] Output = [56, 43, 12, 67, 69, 34]
Using the filter() Method (Recommended)
The filter() method creates a new array with elements that pass a test. This is the most efficient approach for removing negatives:
<!DOCTYPE html>
<html>
<head>
<title>Filter Negative Numbers</title>
</head>
<body>
<p id="result"></p>
<script>
const array = [-2, 5, -7, 32, 78, -32];
const positives = array.filter(num => num >= 0);
document.getElementById("result").innerHTML =
"Original: [" + array + "]<br>" +
"Filtered: [" + positives + "]";
</script>
</body>
</html>
Original: [-2,5,-7,32,78,-32] Filtered: [5,32,78]
Using the splice() Method
The splice() method adds or removes elements from an array and modifies the original array. When removing negatives, iterate backwards to avoid index issues:
Syntax
array.splice(index, deleteCount, item1, ..., itemN)
Example
<!DOCTYPE html>
<html>
<head>
<title>Splice Method</title>
</head>
<body>
<button onclick="removeNegatives()">Remove Negatives</button>
<p id="result"></p>
<script>
function removeNegatives() {
const arr = [9, 38, -25, 42, -12, 98];
// Iterate backwards to avoid index shifting issues
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i] < 0) {
arr.splice(i, 1);
}
}
document.getElementById("result").innerHTML =
"Array after removing negatives: [" + arr + "]";
}
</script>
</body>
</html>
Array after removing negatives: [9,38,42,98]
Using sort() with pop() Method
This approach sorts the array in descending order, placing negatives at the end, then removes them with pop():
<!DOCTYPE html>
<html>
<head>
<title>Sort and Pop Method</title>
</head>
<body>
<p id="result"></p>
<script>
const array = [45, 23, 12, -4, 76, 56, 43, -9];
// Sort in descending order (negatives go to end)
array.sort((a, b) => b - a);
// Remove negatives from the end
while (array.length > 0 && array[array.length - 1] < 0) {
array.pop();
}
document.getElementById("result").innerHTML =
"Array after removing negatives: [" + array + "]";
</script>
</body>
</html>
Array after removing negatives: [76,56,45,43,23,12]
Comparison
| Method | Modifies Original | Performance | Maintains Order |
|---|---|---|---|
filter() |
No | Best | Yes |
splice() |
Yes | Good | Yes |
sort() + pop() |
Yes | Slower | No (sorts array) |
Conclusion
Use filter() for creating a new array without negatives as it's clean and efficient. Use splice() when you need to modify the original array while preserving order.
