The filter method returns a new array containing only the elements that pass the test in the callback function.
Which value should the callback function return to keep an element?
Atrue
Bfalse
Cnull
Dundefined
✗ Incorrect
Returning true keeps the element in the new filtered array.
Does filter change the original array?
AOnly if you assign it back
BYes, it modifies the original array
CNo, it returns a new array
DSometimes, depends on the callback
✗ Incorrect
filter always returns a new array and does not modify the original.
What will [1, 2, 3].filter(n => n > 2) return?
A[1, 2, 3]
B[3]
C[1, 2]
D[]
✗ Incorrect
Only the number 3 is greater than 2, so the new array contains [3].
Which of these is a valid use of filter?
Aarray.filter(element => element.length)
Barray.filter(element => element * 2)
Carray.filter(element => console.log(element))
Darray.filter(element => element === 5)
✗ Incorrect
The callback must return a boolean. element === 5 returns true or false, so it is valid.
Explain how the filter method works and what it returns.
Think about how you pick items from a list based on a rule.
You got /4 concepts.
Write a simple example using filter to select only strings longer than 3 characters from an array.
Use <code>str.length > 3</code> inside the callback.
You got /4 concepts.
Practice
(1/5)
1. What does the JavaScript filter method do?
easy
A. Sorts the array in ascending order
B. Creates a new array with elements that pass a test
C. Changes the original array by removing elements
D. Combines all elements into a single value
Solution
Step 1: Understand the purpose of filter
The filter method tests each element with a function and keeps only those that return true.
Step 2: Compare options with filter behavior
Only Creates a new array with elements that pass a test describes creating a new array with elements that pass the test, which matches filter's behavior.
Final Answer:
Creates a new array with elements that pass a test -> Option B
Quick Check:
filter creates new filtered array = C [OK]
Hint: Filter keeps items passing test, original array stays same [OK]
Common Mistakes:
Thinking filter changes original array
Confusing filter with sort or reduce
Believing filter combines elements
2. Which of the following is the correct syntax to filter an array arr for values greater than 10?
easy
A. arr.filter(x => {x > 10});
B. arr.filter(x > 10);
C. arr.filter(x => x > 10);
D. arr.filter(x => x > 10)();
Solution
Step 1: Check arrow function syntax inside filter
The filter method requires a function that returns true or false. The arrow function must return the condition directly or with a return statement.
Step 2: Validate each option's syntax
arr.filter(x => x > 10); uses correct arrow function syntax returning x > 10. arr.filter(x > 10); misses the function. arr.filter(x => {x > 10}); uses braces but no return, so returns undefined. arr.filter(x => x > 10)(); adds extra parentheses causing error.
Final Answer:
arr.filter(x => x > 10); -> Option C
Quick Check:
Correct arrow function syntax = A [OK]
Hint: Arrow function must return condition inside filter [OK]
Common Mistakes:
Omitting arrow function syntax
Using braces without return in arrow function
Adding extra parentheses after filter call
3. What is the output of the following code?
const nums = [5, 12, 8, 130, 44];
const result = nums.filter(num => num > 10);
console.log(result);
medium
A. [5, 8]
B. []
C. [5, 12, 8, 130, 44]
D. [12, 130, 44]
Solution
Step 1: Identify the filter condition
The filter keeps numbers greater than 10.
Step 2: Check each number in the array
5 (no), 12 (yes), 8 (no), 130 (yes), 44 (yes). So filtered array is [12, 130, 44].
Final Answer:
[12, 130, 44] -> Option D
Quick Check:
Filter > 10 = [12,130,44] [OK]
Hint: Filter keeps only items passing condition [OK]
Common Mistakes:
Including numbers not greater than 10
Returning original array instead of filtered
Confusing filter with map or reduce output
4. Find the error in this code that tries to filter even numbers: