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
JavaScript Arrays
JavaScript Array Methods
JavaScript Objects
Exercises & Assignments
Write a JavaScript program to find the sum and average of array elements
Write a JavaScript program to find the largest and smallest number in an array
Write a JavaScript program to remove duplicates from an array
Write a JavaScript program to filter even numbers and double them
Write a JavaScript program to count the frequency of words in a sentence
Interview Questions & Answers
What is the difference between map and forEach?
What is the difference between slice and splice?
What is the difference between shallow copy and deep copy in JavaScript?
What is the difference between spread and rest operators?
What are Map and Set in JavaScript?
How do you check if a variable is an array in JavaScript?