Array Filter Count
Predict Output Easy
Question
What does this code print?
Code
let r = [1,2,3,4].filter(x => x > 2);
console.log(r.length);
Hint
filter keeps elements where the callback returns true.
Explanation
filter tests each element: 1>2 false, 2>2 false, 3>2 true, 4>2 true. The result is [3, 4] with length 2. filter creates a new array without modifying the original.
Related Resources
Related Lessons
- Arrays
Learn JS arrays.
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.