Sobes.tech
Middle

Mis vahe on meetoditel some, find ja includes?

sobes.tech AI

Vastus AI-lt

In JavaScript, the methods some, find, and includes are applied to arrays, but they serve different purposes:

  • some(callback) — checks if at least one element in the array satisfies the condition in callback. Returns true or false.

  • find(callback) — returns the first element in the array that satisfies the condition in callback. If none, returns undefined.

  • includes(value) — checks if the array contains a specific value. Returns true or false.

Example:

const arr = [1, 2, 3, 4];

arr.some(x => x > 3); // true, there is an element > 3
arr.find(x => x > 3); // 4, first element > 3
arr.includes(3);     // true, 3 is in the array