Open In App

Print all Negative numbers in a Range in JavaScript Array

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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]);
    }
}


Output

-1
-4
-55

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);
    }
});


Output

-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.

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);
    }
}


Output

-1
-4
-55
-66


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads