The original array does not change when you use filter.
If no elements pass the test, filter returns an empty array.
You can use filter with any condition you want, making it very flexible.
Summary
Filter helps you create a new list with only the items you want.
It uses a function that returns true or false for each item.
The original list stays the same after filtering.
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: