Interview question
What is the difference between find(), findIndex(), some(), and every()? find(), findIndex(), some(), और every() में क्या अंतर है?
Answer
| Method | Returns | Stops early? |
|---|---|---|
| find() | The first matching element, or undefined | Yes, on first match |
| findIndex() | Index of first match, or -1 | Yes, on first match |
| some() | true if ANY element matches | Yes, on first true |
| every() | true only if ALL elements match | Yes, on first false |
const users = [
{ id: 1, name: 'John', active: true },
{ id: 2, name: 'Jane', active: false },
{ id: 3, name: 'Bob', active: true }
];
// find - returns the actual element
const inactiveUser = users.find(u => !u.active);
console.log(inactiveUser); // { id: 2, name: 'Jane', active: false }
const notFound = users.find(u => u.id === 99);
console.log(notFound); // undefined
// findIndex - returns the position
const index = users.findIndex(u => u.name === 'Bob');
console.log(index); // 2
console.log(users.findIndex(u => u.name === 'Nobody')); // -1
// some - true if AT LEAST ONE matches (like logical OR across the array)
const hasInactive = users.some(u => !u.active);
console.log(hasInactive); // true
// every - true only if ALL match (like logical AND across the array)
const allActive = users.every(u => u.active);
console.log(allActive); // false - Jane is inactive
// Practical use: validating a form's fields
const fields = [{ valid: true }, { valid: true }, { valid: false }];
const formIsValid = fields.every(f => f.valid);
console.log(formIsValid); // false
// Practical use: checking permission - does user have ANY admin role?
const roles = ['editor', 'viewer'];
const isAdmin = roles.some(role => role === 'admin');
console.log(isAdmin); // false
// Performance note: all four short-circuit, stopping as soon as the answer is known
// - useful for large arrays where you don't need to process every element| Method | Return | जल्दी रुकता है? |
|---|---|---|
| find() | पहला matching element, या undefined | हाँ, पहले match पर |
| findIndex() | पहले match का index, या -1 | हाँ |
| some() | true अगर कोई एक match करे | हाँ |
| every() | true सिर्फ अगर सभी match करें | हाँ |
const users = [
{ id: 1, name: 'John', active: true },
{ id: 2, name: 'Jane', active: false },
{ id: 3, name: 'Bob', active: true }
];
// find - actual element return करता है
const inactiveUser = users.find(u => !u.active);
console.log(inactiveUser); // { id: 2, ... }
// findIndex - position return करता है
const index = users.findIndex(u => u.name === 'Bob');
console.log(index); // 2
// some - कम से कम एक match हो तो true
const hasInactive = users.some(u => !u.active);
console.log(hasInactive); // true
// every - सभी match हों तभी true
const allActive = users.every(u => u.active);
console.log(allActive); // false
// Form validation
const fields = [{ valid: true }, { valid: true }, { valid: false }];
const formIsValid = fields.every(f => f.valid);
console.log(formIsValid); // false
// Permission check
const roles = ['editor', 'viewer'];
const isAdmin = roles.some(role => role === 'admin');
console.log(isAdmin); // falseWas this answer clear?