Predict Array filter Output
Predict Output Easy
Question
What does this code print?
Code
const nums = [1, 2, 3, 4, 5, 6];
const evens = nums.filter(n => n % 2 === 0);
console.log(evens);
Hint
filter keeps elements where the callback returns true.
Explanation
filter creates a new array containing only elements for which the callback returns true. The callback n => n % 2 === 0 is true for even numbers. From [1, 2, 3, 4, 5, 6], the even numbers are [2, 4, 6].
Related Resources
Related Lessons
- JavaScript Arrays
Learn about filter.
Related Reference
- JavaScript Array Reference
Reference for filter.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.