Implement arrayForEach (no prototype mutation)
Arguments
arr: Array<any> —Source array to iterate. Sparse slots must be skipped. callbackFn: (value:any, index:number, array:any[]) => void —Function to invoke for each present element. thisArg: any (optional) —Value of thisinsidecallbackFn.
Returns
void — undefined).Solution idea
A correct polyfill mirrors the ECMAScript behavior: skip holes, use length snapshotting, and validate the callback before iterating.
// Sum values
let sum = 0; arrayForEach([1,2,3], v => { sum += v; }); // sum = 6
// Respect thisArg
const ctx = { seen: [] }; arrayForEach(['a','b'], function (v) { this.seen.push(v); }, ctx); // ctx.seen = ['a','b']
// Skip holes
const a = [1, , 3]; arrayForEach(a, (v, i) => { /* called for i=0 and i=2 only */ });Public interview reports
These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.
- Medium: ServiceNow software engineer (UI) interview experience, 2025
One UI engineer account describes implementing polyfills of built-in methods with attention to edge cases in the deeper JavaScript round. forEach is a representative drill, not the reported method.
Reported in a public ServiceNow interview account. ServiceNow frontend interview questions
Solution
Overview
A correct polyfill mirrors the ECMAScript behavior: skip holes, use length snapshotting, and validate the callback before iterating.
Single approach: length snapshot + own-property check (recommended)
If length changes during iteration, native forEach uses the original length; you can mention this even if the simplified solution doesn’t implement it fully. Include a note about converting array-like values with Object(this).
export default function arrayForEach(arr, callbackFn, thisArg) {
if (!Array.isArray(arr)) throw new TypeError('arr must be an array');
if (typeof callbackFn !== 'function') throw new TypeError('callbackFn must be a function');
const len = arr.length; // snapshot once
const hasOwn = Object.prototype.hasOwnProperty;
for (let i = 0; i < len; i++) {
if (hasOwn.call(arr, i)) {
callbackFn.call(thisArg, arr[i], i, arr);
}
}
return undefined;
}
export default function arrayForEach<T>(
arr: (T | undefined)[],
callbackFn: (value: T, index: number, array: (T | undefined)[]) => void,
thisArg?: any,
): void {
if (!Array.isArray(arr)) throw new TypeError('arr must be an array');
if (typeof callbackFn !== 'function') throw new TypeError('callbackFn must be a function');
const len = arr.length; // snapshot once
const hasOwn = Object.prototype.hasOwnProperty;
for (let i = 0; i < len; i++) {
if (hasOwn.call(arr, i)) {
callbackFn.call(thisArg, arr[i] as T, i, arr);
}
}
}
Notes & Pitfalls
If you use for (let i = 0; i < arr.length; i++), you may accidentally iterate elements appended during iteration.If you don’t check for existence, you’ll call the callback on holes (native forEach skips them). thisArgwon’t affect arrow functions (they don’t bindthis).
Sparse arrays like [1, , 3]: callback runs for indices 0 and 2 only.Delete-before-visit: if index 1 is deleted while processing index 0, index 1 must be skipped. Push-during-iteration: elements appended after the call starts must not be visited.
Snapshot length once. Use hasOwnProperty.call(arr, i)to skip holes and to respect delete-before-visit.Use .call(thisArg, value, index, array)to bind context.
Common mistakes on this challenge
arrayForEach loop semantics are off
forEach-like behavior requires stable order, optional thisArg binding, and sparse-array awareness. Validate arris an array andcallbackFnis a function.Snapshot arr.lengthonce before iteration.Visit only existing indices and call callbackFn.call(thisArg, value, index, arr).
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.