A shop has 2,000 products and a customer ticks "in stock" and "under $50". How does the list shrink to just the matches?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
filter keeps the elements for which the callback returns true.
const prices = [12, 75, 30, 120, 8];
const affordable = prices.filter((p) => p < 50);
console.log(affordable);
console.log(prices.some((p) => p > 100), prices.every((p) => p > 5));
03Objective and prerequisites
Learning objective: Use filter to keep the elements that pass a test, write clear predicate functions, combine conditions, and use some and every to answer yes/no questions about a list.
Skills practised: Functional array processing, Selecting with filter.
04Explanation
list.filter(test) returns a new array containing only the elements for which test returns true. The original array is unchanged.
The test function is called a predicate: it answers a yes/no question about one element, such as (p) => p.inStock.
Combine conditions inside the predicate with && and ||: (p) => p.inStock && p.price < 50.
some(test) is true if at least one element passes; every(test) is true if all of them do. Both stop as soon as they know the answer.
filter uses the truthiness of the callback's return value, so list.filter(Boolean) removes falsy values (0, "", null, undefined, NaN, false). Be careful when 0 is valid data.
Named predicates (const isAffordable = (p) => p.price < 50) document intent and can be reused and tested. Build complex filters from small predicates.
every on an empty array is true and some is false (vacuous truth). Check the length when "no data" must be treated differently.
Filtering then counting (filter(...).length) allocates an array; for very large data a loop or reduce counts without allocating.
05Definitions
filter
Returns a new array with only the elements that pass a test.
Predicate
A function that returns true or false for one value.
some
True when at least one element passes the test.
every
True when all elements pass the test.
Falsy
A value treated as false: 0, "", null, undefined, NaN or false.
Each tick box on a shop's filter panel is one predicate. The visible list is the catalogue filtered by all active predicates, and the original catalogue is never changed, so unticking a box restores results instantly.
console.log([1, 2, 3, 4].filter((n) => n % 2 === 0));
[2, 4]
[1, 3]
[true, false]
2
2. What does a predicate return?
A new array
true or false (truthy/falsy)
An index
Nothing
3. What is printed?
console.log([1, 8, 3].some((n) => n > 5));
true
false
4. What is printed?
console.log([].every((n) => n > 5));
true
false
5. Why are zero-quantity lines kept AND valid 0 prices removed?
const prices = [0, 5, null, 9].filter(Boolean);
filter(Boolean) removes every 0
Boolean is not a function
filter mutates
Nothing is wrong
6. You only need to know whether any order is overdue. Which is best?
orders.filter(isOverdue).length > 0
orders.some(isOverdue)
orders.map(isOverdue)
orders.every(isOverdue)
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Compare, do not assign: Prints 2 closed
Search box: Prints the matches; Uses filter
Rental search filters: Prints matches and checks; hasBeds returns a predicate; underBudget is strict
ex-033-01: output matches the documented expected output
ex-033-02: output matches the documented expected output
ex-033-03: output matches the documented expected output
ex-033-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-07-functional-array-processing/lesson-033-filter-and-selection/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
filter keeps the elements whose predicate returns true.
Combine conditions with && and || in the predicate.
some: at least one; every: all of them.
Named predicates make filters readable and testable.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationfilter, some and every
Context: I am learning filter, some and every in JavaScript.
Task: Explain filter, predicates, some and every with a product search example.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionfilter, some and every drill
Context: I have practised filter, some and every.
Task: Give me six filter, some and every calls and ask for the results, including empty arrays.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingfilter, some and every bug
Context: My filter keeps every item and also changes my data.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationfilter, some and every practice
Context: I finished the filter, some and every exercises.
Task: Create three filtering exercises with combined conditions.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewfilter, some and every review
Context: Here is my code that uses filter, some and every.
Task: Review my filters and suggest named predicates where conditions are hard to read.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.