Array Filter Result
Predict Output Medium
Question
What does this code print?
Code
let nums = [1, 2, 3, 4, 5];
let 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 with elements that pass the test. 1%2=1 (false), 2%2=0 (true), 3%2=1 (false), 4%2=0 (true), 5%2=1 (false). Only 2 and 4 pass. The output is: [ 2, 4 ]
Related Resources
Related Lessons
- Arrays
Learn how arrays work.
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.