JavaScript Array prototype Constructor
Last Updated :
13 Jul, 2023
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",
"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.
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 = ["GeeksforGeeks", "GFG", "myGeeks"];
str.stringLength();
console.log(str);
}
lengthFunction();
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.
Similar Reads
JavaScript Array constructor Property The JavaScript Array constructor property is used to return the constructor function for an array object. It only returns the reference of the function and does not return the name of the function. In JavaScript arrays, it returns the function Array(){ [native code] }.Syntax: array.constructorReturn
2 min read
JavaScript Array() Constructor The Array() constructor is used to create Array objects and the array constructor can be called with or without a new keyword, both can create a new Array.Syntax:new Array(Value1, Value2, ...);new Array(ArrayLength);Array(Value1, Value2, ...);Array(ArrayLength);Parameters: ValueN: An array initializ
2 min read
JavaScript ArrayBuffer constructor Property JavaScript ArrayBuffer constructor property is used to return the ArrayBuffer constructor function for the object. The function returned by this property is just the reference, not the actual ArrayBuffer. It is an object property of JavaScript and can be used with Strings, Numbers, etc. Syntax: arra
1 min read
JavaScript Object Constructors An object is the collection of related data or functionality in the form of key. These functionalities usually consist of several functions and variables. All JavaScript values are objects except primitives. const GFG = { subject : "programming", language : "JavaScript",}Here, subject and language a
4 min read
What is a Constructor in JavaScript? A constructor in JavaScript is a special function that is used to create and initialize objects. When we want to create multiple objects with similar properties and methods, the constructor is used as a blueprint to create similar objects. This is useful when you want to create multiple objects with
8 min read