Print all Negative numbers in a Range in JavaScript Array
Last Updated :
13 Feb, 2024
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]
Output: -22, -88, -69, all negative numbers lies inside the range
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 it is a negative number.
Syntax:
for (initialization; condition; increment/decrement) {
// code
}
Example: The below code example uses the for loop to print all negative numbers in a range of -60 to -1 in a JavaScript array.
JavaScript
let arr = [12, 34, 13, -1, -4, 45, -55, -66];
let start = -60;
let end = -1;
for (let i = 0; i < arr.length; i++) {
if (arr[i] < 0 &&
arr[i] >= start &&
arr[i] <= end)
{
console.log(arr[i]);
}
}
Using forEach method
In this approach, we are using the forEach method to iterate over each element of the array to get each of the negative numbers.
Syntax:
array.forEach(function(currentValue, index, array) {
// code
});
Example: The below code example uses the forEach method to print all negative numbers in a range of -70 to -1 in a JavaScript array.
JavaScript
let arr =
[12, 34, 13, -1, -4, 45, -55, -66];
let start = -70;
let end = -1;
arr.forEach(number => {
if (number < 0 && number >= start && number <= end) {
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.
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 range of -70 to -1 in a JavaScript array.
JavaScript
let arr =
[12, 34, 13, -1, -4, 45, -55, -66];
let start = -70;
let end = -1;
let res =
arr.filter(number =>
number < 0 && number >= start &&
number <= end);
console.log(res);
Output[ -1, -4, -55, -66 ]
Using for...of Loop
In this approach, we are using the for...of 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 range of -70 to -1 in a JavaScript array.
JavaScript
let arr = [12, 34, 13, -1, -4, 45, -55, -66];
let start = -70;
let end = -1;
for (let number of arr) {
if (number < 0 && number >= start
&& number <= end) {
console.log(number);
}
}
Similar Reads
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 numbe
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
Print all Positive Numbers in a Range in JavaScript Array To print all the possible numbers in a range from the JavaScript array, we need to iterate over the array and apply the condition to filter out the positive 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]Output
3 min read
JavaScript Program to Count Positive and Negative Numbers in an Array Given an array of numbers. Write a JavaScript program to count positive and negative numbers in an array.Example:Input: numbers_array1= [10,20, -1,22,99,20, -9]Output: positive no's=5, negative no's =2Input: numbers_array2= [-121, - 78, -13, 54, -23]Output: positive no's=1, negative no's=4Brute Forc
2 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
JavaScript program to print even numbers in an array Given an array of numbers and the task is to write a JavaScript program to print all even numbers in that array. We will use the following methods to find even numbers in an array: Table of Content Method 1: Using for Loop Method 2: Using while Loop Method 3: Using forEach LoopMethod 4: Using filter
5 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
JavaScript Number NEGATIVE_INFINITY Property In JavaScript, NEGATIVE_INFINITY is a property of a number object which represents negative infinity (-Infinity). It can be explained as a number that is lower than any other number. It is a Read-Only property. Syntax: NEGATIVE_INFINITY is a static property of the NUMBER object and hence is always u
1 min read
Javascript Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9};
4 min read