The invocation context (this) of the forEach function call

MDN states:

array.forEach(callback[, thisArg])

If a thisArg parameter is provided to forEach, it will be used as the
this value for each callback invocation as if callback.call(thisArg,
element, index, array) was called. If thisArg is undefined or null,
the this value within the function depends on whether the function is
in strict mode or not (passed value if in strict mode, global object
if in non-strict mode).

So in short, if you only provide the callback and you’re in non-strict mode (the case you presented), it will be the global object (window).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Leave a Comment