Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 3 of 10 · Array Methods
Interview question

What is the difference between find(), findIndex(), some(), and every()? find(), findIndex(), some(), और every() में क्या अंतर है?

Answer
MethodReturnsStops early?
find()The first matching element, or undefinedYes, on first match
findIndex()Index of first match, or -1Yes, on first match
some()true if ANY element matchesYes, on first true
every()true only if ALL elements matchYes, 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
MethodReturnजल्दी रुकता है?
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); // false

Was this answer clear?