# Your First Polyfill

DevFeed: [Your First Polyfill](<https://devfeed.tech/articles/your-first-polyfill-52597.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/writing-javascript-polyfill/>)

Published: 2012-06-20T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [polyfill](<https://devfeed.tech/topics/polyfill.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>), [forEach](<https://devfeed.tech/topics/foreach.md>), [Tutorial](<https://devfeed.tech/topics/tutorial.md>)

Tags: [array](<https://devfeed.tech/tags/array.md>), [browser](<https://devfeed.tech/tags/browser.md>), [ecmascript](<https://devfeed.tech/tags/ecmascript.md>), [fallback](<https://devfeed.tech/tags/fallback.md>), [foreach](<https://devfeed.tech/tags/foreach.md>), [html5](<https://devfeed.tech/tags/html5.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [js](<https://devfeed.tech/tags/js.md>), [mdn](<https://devfeed.tech/tags/mdn.md>), [polyfill](<https://devfeed.tech/tags/polyfill.md>), [polyfills](<https://devfeed.tech/tags/polyfills.md>)

## AI overview

A polyfill supplies a fallback when a browser lacks a feature; the tutorial recommends checking for native support before adding one. Its illustrative Array.prototype.forEach implementation validates that the callback is a function, saves the array length, then calls the callback for each item with the item, index, and array. It uses callback.call(thisArg, ...) to set the callback's this value. The author notes this is a deliberately simple, incomplete implementation and points to MDN for a more comprehensive version.

## Source excerpt

In today's tutorial I want to introduce you to the concepts behind polyfills, a word you see used a lot in today's JS world. I'll demonstrate just what a polyfill is and then write one ourselves. A polyfill is a piece of code that provides a fallback if a certain feature doesn't exist within that browser's JS engine. Polyfills usually follow a pattern. First, they check to see if the function they implement exists, and then we only write our fallback implementation if we have to. There are multiple polyfills out there for mutliple functions. The website HTML5 Please is very useful for finding polyfills to do a particular job. Now, lets get to our own implementation. I should note as this point that this implementation is by no means going to be a fully fledged, comprehensive one. At the end, I'll link you to the Mozilla Documentation Network (or MDN) page that contains a hugely comprehensive & fully featured polyfill, should you require it. Today we'll be implementing Array forEach, which was introduced in JavaScript 1.6, ECMAScript 5th edition. In reality, this is actually a very well supported feature, but I've chosen it more for the fact that it's a fairly simple implementation. The first thing we need to do is see if the method has been natively implemented. We do this by checking to see if Array.prototype.forEach != undefined. If it is indeed undefined, we can continue. What this function does is iterate through all items within an array and call a function on them. This function is passed 3 arguments: the item, the index & the array it's iterating on. It's also possible to pass in to forEach a second value, which will be used as the value for this within the callback. With that in mind, lets implement it! At this point, I'm presuming the method is not natively supported, and we need to implement it. Our first line simply defines the function: Array.prototype.forEach = function(callback, thisArg) { Next, we need to check if callback is a function or not, and th