Open In App

JavaScript Array prototype Constructor

The JavaScript array prototype constructor is used to allow to add new methods and properties to the Array() object. If the method is constructed, then it will be available for every array. When constructing a property, All arrays will be given the property, and its value, as default.

Syntax:



Array.prototype.name = value

Note: It does not refer to a single array, but to the Array() object itself, It means Array.prototype itself is an Array.

Below are examples of Array prototype Constructor:



Example 1: In this example, we will use Array prototype Constructor to convert the array to lowercase.




Array.prototype.lowerCase = function () {
    let i;
    for (i = 0; i < this.length; i++) {
        this[i] = this[i].toLowerCase();
    }
};
 
function myGeeks() {
    let sub = ["DSA", "WEBTEchnologies",
        "GeeksforGeeks", "gfg"];
    sub.lowerCase();
 
    console.log(sub);
}
myGeeks();

Output
[ 'dsa', 'webtechnologies', 'geeksforgeeks', 'gfg' ]

Example 2: This example uses a JavaScript array prototype constructor and converts string characters into upper-case characters. 




Array.prototype.upperCase = function () {
    let i;
    for (i = 0; i < this.length; i++) {
        this[i] = this[i].toUpperCase();
    }
};
 
function myGeeks() {
    let sub = ["Algorithm", "Data Structure",
        "Operating System", "html"];
    sub.upperCase();
    console.log(sub);
}
myGeeks();

Output
[ 'ALGORITHM', 'DATA STRUCTURE', 'OPERATING SYSTEM', 'HTML' ]

Example 3: This example uses a JavaScript array prototype constructor to count string length. 




Array.prototype.stringLength = function () {
    let i;
    for (i = 0; i < this.length; i++) {
        this[i] = this[i].length;
    }
};
 
function lengthFunction() {
    let str = ["GeeksforGeeks", "GFG", "myGeeks"];
    str.stringLength();
    console.log(str);
}
lengthFunction();

Output
[ 13, 3, 7 ]

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers: The browsers supported by JavaScript Array Prototype Constructor are listed below: 

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  


Article Tags :