Wednesday, July 3, 2024
HomeLanguagesJavascriptJavaScript Array prototype Constructor

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.

Javascript




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


Output

[ 'dsa', 'webtechnologies', 'neveropen', 'gfg' ]

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

Javascript




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. 

Javascript




Array.prototype.stringLength = function () {
    let i;
    for (i = 0; i < this.length; i++) {
        this[i] = this[i].length;
    }
};
 
function lengthFunction() {
    let str = ["neveropen", "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: 

  • Google Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Safari
  • Opera

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.  

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments