What does the find() method do in JavaScript arrays?
The find() method returns the first element in an array that satisfies a given condition (a test function). If no element matches, it returns undefined.
Click to reveal answer
beginner
What is the purpose of the some() method in JavaScript arrays?
The some() method checks if at least one element in the array passes a test function. It returns true if any element matches, otherwise false.
Click to reveal answer
intermediate
How does find() differ from filter()?
find() returns only the first matching element, while filter() returns all matching elements as a new array.
Click to reveal answer
beginner
What will some() return if the array is empty?
It will return false because there are no elements to satisfy the condition.
Click to reveal answer
beginner
Write a simple example using find() to get the first even number from [1, 3, 4, 6].
What does find() return if no element matches the condition?
Aundefined
Bnull
Cfalse
Dempty array
✗ Incorrect
find() returns undefined when no element satisfies the condition.
Which method returns a boolean indicating if any array element passes a test?
Asome()
Bfind()
Cfilter()
Dmap()
✗ Incorrect
some() returns true or false depending on whether any element passes the test.
What will [2, 4, 6].some(x => x > 5) return?
Aundefined
Bfalse
Ctrue
D6
✗ Incorrect
Since 6 is greater than 5, some() returns true.
Which method would you use to get all elements that match a condition?
Afind()
Bsome()
Creduce()
Dfilter()
✗ Incorrect
filter() returns all elements that satisfy the condition as a new array.
If you want only the first matching element from an array, which method is best?
Afilter()
Bfind()
Csome()
Dmap()
✗ Incorrect
find() returns the first element that matches the condition.
Explain how the find() method works and give an example.
Think about searching for something in a list and stopping when you find it.
You got /3 concepts.
Describe the difference between some() and find() methods.
One tells you if something exists, the other gives you the actual item.
You got /4 concepts.
Practice
(1/5)
1. What does the find method do in JavaScript arrays?
easy
A. Returns the first element that matches a condition or undefined if none
B. Returns true if all elements match a condition
C. Returns the index of the last matching element
D. Returns a new array with elements that match a condition
Solution
Step 1: Understand the purpose of find
The find method searches an array and returns the first element that satisfies the given condition.
Step 2: Compare with other methods
Unlike filter which returns all matching elements, find returns only the first or undefined if none found.
Final Answer:
Returns the first element that matches a condition or undefined if none -> Option A
Quick Check:
find returns first match [OK]
Hint: Remember: find returns one item, some returns true/false [OK]
Common Mistakes:
Confusing find with filter
Thinking find returns a boolean
Assuming find returns all matches
2. Which of the following is the correct syntax to use some on an array arr to check if any element is greater than 10?
easy
A. arr.some(x > 10);
B. arr.some(x => x > 10);
C. arr.some(x => {x > 10});
D. arr.some(x => x > 10) {};
Solution
Step 1: Identify correct arrow function syntax
The some method requires a function that returns true or false. The arrow function x => x > 10 correctly returns a boolean.
Step 2: Check syntax errors in options
arr.some(x > 10); misses the arrow function syntax. arr.some(x => {x > 10}); uses braces but no return statement, so returns undefined. arr.some(x => x > 10) {}; has extra braces after the method call, which is invalid.
Final Answer:
arr.some(x => x > 10); -> Option B
Quick Check:
Arrow function with return expression [OK]
Hint: Use arrow function with expression for some() [OK]
Common Mistakes:
Missing arrow in arrow function
Using braces without return in arrow function
Adding extra braces after method call
3. What is the output of this code?
const numbers = [3, 7, 12, 5];
const result = numbers.find(n => n > 10);
console.log(result);
medium
A. 7
B. undefined
C. 3
D. 12
Solution
Step 1: Understand the find condition
The condition n > 10 looks for the first number greater than 10 in the array.
Step 2: Check array elements in order
The array is [3, 7, 12, 5]. The first element > 10 is 12.
Final Answer:
12 -> Option D
Quick Check:
First number > 10 is 12 [OK]
Hint: Find returns first match, not all matches [OK]
Common Mistakes:
Thinking find returns all matches
Confusing find with some
Choosing first element instead of first matching
4. The following code does not work as expected. What is the problem?