PROWAREtech

articles » current » javascript » ie11 » ie11-prototype-array-fill

JavaScript: IE11 Array.fill() Method/Function

Create the Array.fill() function for Internet Explorer 11.

Still developing for IE11? Well, the fill() method in JavaScript is used to fill all the elements of an array from a start index to an end index with a static value.


if (!Array.prototype.fill) {
	Array.prototype.fill = function (value) {

		if (this == null) {
			throw new Error('this is null or not defined');
		}

		var O = Object(this);

		var len = O.length >>> 0;

		var start = arguments[1];
		var relativeStart = start >> 0;

		var k = relativeStart < 0 ?
			Math.max(len + relativeStart, 0) :
			Math.min(relativeStart, len);

		var end = arguments[2];
		var relativeEnd = end === undefined ?
			len : end >> 0;

		var final = relativeEnd < 0 ?
			Math.max(len + relativeEnd, 0) :
			Math.min(relativeEnd, len);

		while (k < final) {
			O[k] = value;
			k++;
		}

		return O;
	};
}

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE