Print all Negative Numbers in JavaScript Array
Last Updated :
17 Jul, 2024
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:
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]);
}
}
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);
}
});
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);
Output[ -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);
}
}
Using reduce Method
In this approach, we use the reduce method to iterate over the array, accumulate the negative numbers into a new array, and then print the resulting array.
Example: The below code example uses the reduce method to collect and print all negative numbers in a JavaScript array.
JavaScript
let arr = [12, 34, 13, -1, -4, 45, -55, -66];
let negativeNumbers = arr.reduce((accumulator, number) => {
if (number < 0) {
accumulator.push(number);
}
return accumulator;
}, []);
console.log(negativeNumbers);
Similar Reads
Print all Negative numbers in a Range in JavaScript Array To print all the negative numbers in a range 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. Example:Input: arr = [-22, 45, 63, -88, -69]Range: [-1 to -70]Outpu
3 min read
Print all Positive Numbers in JavaScript Array In JavaScript, arrays are the data structures used to store multiple elements. In some cases, we need to extract the elements that satisfy a specified condition. We can use a condition to extract all the positive numbers from an array with the below approaches. Example:Input: arr = [-1, -12, 35, -8,
3 min read
Convert a negative number to positive in JavaScript In this article, we will see how we can convert a negative number to a positive number in JavaScript by the methods described below. Below are the methods to convert a negative number to a positive in JavaScript: Table of Content Multiplying by -1Using Math.abs()adding a minus signFlipping the bitUs
4 min read
Print all Odd Numbers in a Range in JavaScript Array Odd numbers are numbers that cannot be divided into equal parts. If we divide the odd number by 2, the remainder is 1. In JavaScript, if we want to print all Odd numbers in a range, we can print it by iterating over the array, applying the condition, and printing the odd numbers. There are various a
4 min read
Print all Even Numbers in a Range in JavaScript Array We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer. There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows: Table of Content Using for Loop in
3 min read