Print all Negative Numbers in JavaScript Array
In JavaScript, arrays are the data structures used to store the elements. In some cases, we need to extract the elements that satisfy the condition. To print all the negative numbers from the JavaScript array, we need to iterate over the array and apply the condition to filter out the negative numbers. Once the elements are extracted, we can print them in the console.
There are various approaches in JavaScript through which one can print all the negative values which are as follows:
Table of Content
- Using for Loop
- Using forEach method
- Using filter method
- Using for…of Loop
Using for Loop
In this approach, we are using the for loop to iterate over each element of the array and check if each element is less than 0, which means a negative number. If the number is negative then we are printing it using the console.log() method.
Syntax:
for (initialization; condition; increment/decrement) {
// code
}
Example: The below code example uses the for Loop to print all negative numbers in a JavaScript array.
Javascript
// Input arr let arr = [12, 34, 13, -1, -4, 45, -55, -66]; // For loop to print negative numbers for (let i = 0; i < arr.length; i++) { if (arr[i] < 0) { // Printing output console.log(arr[i]); } } |
-1 -4 -55 -66
Using forEach method
In this approach, we are using the forEach method to iterate over each element of the array. For each negative numbers, the element is printed to the console on separate lines using the log function.
Syntax:
array.forEach(function(currentValue, index, array) {
// code
});
Example: The below code example uses the forEach method to print all negative numbers in a JavaScript array.
Javascript
// input array let arr = [12, 34, 13, -1, -4, 45, -55, -66]; // Printing negative numbers using forEach method arr.forEach((number) => { if (number < 0) { // Printing result console.log(number); } }); |
-1 -4 -55 -66
Using filter method
In this approach, we are using the built-in filter method to create the new array which consists of only the negative numbers from the input array. The resulting array is printed as the output using the log() function.
Syntax:
const res = array.filter(function(currentValue, index, array) {
// code
});
Example: The below code example uses the filter method to print all negative numbers in a JavaScript array.
Javascript
// Input arr let arr = [12, 34, 13, -1, -4, 45, -55, -66]; // Filtering and printing negative // numbers using filter method let res = arr.filter(number => number < 0); // Printing output console.log(res); |
[ -1, -4, -55, -66 ]
Using for…of Loop
In this approach, we are using the for…of the loop to iterative over each element of the input arr and check if the number of the array is less than 0. If the number is less than 0, then it is printed in the console.
Syntax:
for (variable of iterable) {
// code
}
Example: The below code example uses the for…of Loop to print all negative numbers in a JavaScript array.
Javascript
// Input arr let arr = [12, 34, 13, -1, -4, 45, -55, -66]; // Printing negative numbers using for...of loop for (let number of arr) { if (number < 0) { // Printing output console.log(number); } } |
-1 -4 -55 -66