Related Articles
How to write a function that returns array elements larger than a number in JavaScript ?
• Last Updated : 14 Apr, 2021

Given an array arr and number n and the task is to write a function that returns an array whose items are larger than n.

Example:

```Input:   arr = [65, 16, 0, 6, 64, 1, 68]
n = 16
Output:  [65, 64, 68]

Input:   arr = [6, 46, 54, 6, 56, 54, 65, 4, 65]
n = 50
Output:  [54, 56, 54, 65, 65]```

To achieve this we have the following approaches :

#### Approach 1: Using Array.filter()

In this approach, we use the Array.filter method.  At every iteration, we check if the value is greater than num or not.

Example:

## Javascript

 ``

Output:

```[65, 64, 68]
[54, 56, 54, 65, 65]```

#### Approach 2: Using Array.reduce()

In this approach, we use the array.reduce method. Initialize the accumulator using an empty array and at every iteration check if the current value is greater than num then concatenate the current value with the accumulator and return it otherwise return the accumulator as it is.

Example:

## Javascript

 ``

Output:

```[65, 64, 68]
[54, 56, 54, 65, 65]```

#### Approach 3: Using the Array.map method:

In this approach, we iterate the array using the map method and at every iteration, we check if the current value is greater than num or not? If true, then we return the value as it. If false, then replace the item with an empty string (Or any falsy value). After that using the filter method remove the falsy values.

## Javascript

 ``

Output:

```[65, 64, 68]
[54, 56, 54, 65, 65]```

#### Approach 4: using the for loop:

In this approach, we create an empty array and iterate the given array using the for loop and at every iteration, we check the current value is greater than num or not. If true, then we push the value in the newly created array.

## Javascript

 ``

Output:

```[65, 64, 68]
[54, 56, 54, 65, 65]```
My Personal Notes arrow_drop_up