Write a JavaScript program to filter even numbers and double them

Description: From [1, 2, 3, 4, 5, 6] keep only the even numbers and double each one.

Hint:

  • Use filter() first, then map().

JavaScript filter and map program

const numbers = [1, 2, 3, 4, 5, 6];

const result = numbers
  .filter(n => n % 2 === 0)
  .map(n => n * 2);

console.log(result);

Output:

Tutorials Class - Output Window

[ 4, 8, 12 ]

How it works

  • filter() keeps 2, 4 and 6.
  • map() doubles them to 4, 8 and 12.

Read Array Methods.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.