Lập trình · 23/10/2026

JavaScript find: Does Finding Zero or Undefined Mean No Match?

Using Array.find's result directly in an if can discard a valid zero, false or empty string. When undefined is valid data, comparing the result to undefined is also ambiguous. Separate the matched value from whether a match exists.

JavaScript find: tìm thấy 0 hoặc undefined có phải là không tìm thấy?

Using Array.find's result directly in an if can discard a valid zero, false or empty string. When undefined is valid data, comparing the result to undefined is also ambiguous. Separate the matched value from whether a match exists.

1. A falsy value can be a successful match

const counts = [0, 5, 10];
const match = counts.find(value => value === 0);
console.log(match); // 0
console.log(Boolean(match)); // false
console.log(counts.some(value => value === 0)); // true

As MDN Array.find explains, find returns the first element whose predicate result is truthy, or undefined when none matches. The element itself need not be truthy. The predicate matches zero, while if (match) checks zero's own truthiness.

Use some when the caller only needs existence. When retrieving data from a domain that permits falsy values, preserve match status alongside the result.

2. undefined can be a valid element

const values = [undefined, 'ready'];
const predicate = value => value === undefined;
console.log(values.find(predicate)); // undefined
console.log([].find(predicate)); // undefined
console.log(values.findIndex(predicate)); // 0
console.log([].findIndex(predicate)); // -1

The identical undefined results hide different outcomes. findIndex returns an index or -1, preserving that distinction. Compare the index explicitly with -1; zero is a valid first position.

For ordinary sparse arrays, find and findIndex can also read holes as undefined. Require assigned indices or forbid sparse inputs if only explicit elements may match. These examples use dense arrays.

3. Return a found flag with the value

function findValue(values, predicate) {
  if (!Array.isArray(values) || typeof predicate !== 'function') {
    throw new TypeError('expected an array and a predicate');
  }
  const index = values.findIndex((value, index) => {
    const matched = predicate(value, index);
    if (typeof matched !== 'boolean') {
      throw new TypeError('predicate must return a boolean');
    }
    return matched;
  });
  return index === -1
    ? { found: false }
    : { found: true, index, value: values[index] };
}
const hit = findValue([undefined], value => value === undefined);
console.log(hit.found); // true
console.log(hit.value === undefined); // true
console.log(findValue([], () => true).found); // false

The helper targets ordinary data arrays and synchronous predicates that do not mutate the array during search. It requires an explicit boolean, a stricter policy than the native method's truthy/falsy acceptance. An invoked callback returning a Promise or a number causes TypeError.

For empty arrays, the callback never runs, so its result type cannot be checked. This helper does not prove that an arbitrary callback always returns a boolean. Async callbacks and potentially rejecting work need a separate awaited flow.

4. Object results still share references

When the matching element is an object, value references the object inside the array. Mutating its properties can change the source. The found flag clarifies the result representation; it does not clone records or validate their schemas.

Avoid running some followed by find with a side-effecting predicate to construct one logical result. Two traversals can observe different states and repeat work. Choose a single search that returns the information the caller needs.

5. Review search results

  • Can valid data include zero, false, empty text or undefined?
  • Does the caller need existence, position or the element?
  • Do tests cover a first-position match and no match?
  • Are holes or mutation during search permitted?
  • Is the predicate synchronous and returning the agreed type?

All six bilingual blocks ran with Node.js, with additional falsy-value, first-match, object-identity and invalid-callback-result checks. Real APIs and browser interfaces were not integrated.

Discussion

Comments 0

Sign in to comment

You need an account to join the discussion and reply to other readers.

Sign inRegister

No comments yet. Be the first to share your thoughts.