2.6 Array.prototype.some (Week 2 • JS Arrays)
1. Core Concept & Internal Working
Array.prototype.some (JS Arrays) covers Existential short-circuit check (`true` as soon as AT LEAST ONE item matches; `false` on empty array `[]`). In production software engineering, it is used to Checking `cart.some(item => item.stock === 0)` to disable the Checkout button when any item is out of stock. Mastering its underlying execution rules and edge-case boundaries lets you write reliable, maintainable code.
2. Real-World Engineering Use Case
Checking `cart.some(item => item.stock === 0)` to disable the Checkout button when any item is out of stock.
3. Granular Subtopics Covered
- Existential short-circuit check (`true` as soon as AT LEAST ONE item matches: Core foundation of Array.prototype.some: Checking `cart.some(item => item.stock === 0)` to disable the Checkout button when any item is out of stock.
- `false` on empty array `[]`): Master '`false` on empty array `[]`)' inside Array.prototype.some across real-world inputs.
- Production Pitfall Guard (Array.prototype.some): Prevent common defect: Using `.filter(...).length > 0` (allocates an unnecessary array and scans everything) instead of `.some(...)`.
- Hands-On Verification (Array.prototype.some): Check duplicate email existence and out-of-stock cart warnings using `.some()`.
- Array.prototype.some — Interview Readiness: Explain behavior, trade-offs, and edge cases of Array.prototype.some.
- Array.prototype.some — Interview Readiness: Explain behavior, trade-offs, and edge cases of Array.prototype.some.
4. Reference Implementation
const hasOutOfStock = cartItems.some((item) => item.availableQty < item.qty);
5. Common Mistakes to Avoid
- Using `.filter(...).length > 0` (allocates an unnecessary array and scans everything) instead of `.some(...)`.
- Skipping boundary, null/empty, or error-state verification when implementing Array.prototype.some.
- Pushing untested Array.prototype.some changes without clean naming and Git commit context.
6. Hands-On Exercise
Check duplicate email existence and out-of-stock cart warnings using `.some()`.
7. Technical Interview Checkpoints
- Q: What problem does Array.prototype.some solve in JS Arrays, and what fails if it is misused?
Key Focus: Checking `cart.some(item => item.stock === 0)` to disable the Checkout button when any item is out of stock. Guard against: Using `.filter(...).length > 0` (allocates an unnecessary array and scans everything) instead of `.some(...)`. - Q: Explain the internal working of Array.prototype.some (Existential short-circuit check (`true` as soon as AT LEAST ONE item matches; `false` on empty array `[]`)).
Key Focus: Walk step-by-step through Existential short-circuit check (`true` as soon as AT LEAST ONE item matches; `false` on empty array `[]`) with a concrete code example. - Q: How did you apply and verify Array.prototype.some in your hands-on lab?
Key Focus: Check duplicate email existence and out-of-stock cart warnings using `.some()`.