Skip to content
Related Articles

Related Articles

Improve Article
JavaScript Array prototype Constructor
  • Last Updated : 01 Apr, 2021

Below is the example of Array prototype Constructor to make them lowercase. 
 

  • Example: 
     

html




<!DOCTYPE html>
<html>
<head>
    <title>
        JavaScript Array prototype Constructor
    </title>
</head>
 
<body style = "text-align:center;">
     
    <h1 style = "color:green;" >
        GeeksforGeeks
    </h1>
     
    <button onclick="myGeeks()">
        Click here
    </button>
     
    <p id="GFG" style = "color:Green;"></p>
 
     
    <!-- Script to use JavaScript Array prototype
        Constructor -->
    <script>
        Array.prototype.upperCase = function() {
            var i;
            for (i = 0; i < this.length; i++) {
                this[i] = this[i].toLowerCase();
            }
        };
         
        function myGeeks() {
            var sub = ["DSA", "WEBTEchnologies",
                            "GeeksforGeeks", "gfg"];
            sub.upperCase();
             
            document.getElementById("GFG").innerHTML= sub;
        }
    </script>
</body>
 
</html>
  •  
  • Output:

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 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.
More example codes for the above method are as follows:
Program 1: This example use JavaScript array prototype constructor and convert string character into upper case character. 
 

html




<!DOCTYPE html>
<html>
<head>
    <title>
        JavaScript Array prototype Constructor
    </title>
</head>
 
<body style = "text-align:center;">
     
    <h1 style = "color:green;" >
        GeeksforGeeks
    </h1>
     
    <button onclick="myGeeks()">
        Click here
    </button>
     
    <p id="GFG" style = "color:Green;"></p>
 
     
    <!-- Script to use JavaScript Array prototype
        Constructor -->
    <script>
        Array.prototype.upperCase = function() {
            var i;
            for (i = 0; i < this.length; i++) {
                this[i] = this[i].toUpperCase();
            }
        };
         
        function myGeeks() {
            var sub = ["Algorithm", "Data Structure",
                            "Operating System", "html"];
            sub.upperCase();
             
            document.getElementById("GFG").innerHTML= sub;
        }
    </script>
</body>
 
</html>

Output: 
 



Program 2: This example use JavaScript array prototype constructor to count string length. 
 

html




<!DOCTYPE html>
<html>
     
<head>
    <title>
        JavaScript Array prototype Constructor
    </title>
</head>
 
<body style = "text-align:center;">
 
    <h1 style = "color:green;" >
        GeeksforGeeks
    </h1>
 
    <button onclick="lengthFunction()">
        Click here
    </button>
     
    <p id="GFG"></p>
 
     
    <!-- Script to use Array prototype Constructor
         and count length of string -->
    <script>
        Array.prototype.stringLength = function() {
            var i;
            for (i = 0; i < this.length; i++) {
                this[i] = this[i].length;
            }
        };
         
        function lengthFunction() {
             
            // Declare an array
            var str = ["GeeksforGeeks", "GFG", "myGeeks"];
             
            str.stringLength();
            document.getElementById("GFG").innerHTML = str;
        }
    </script>
</body>
 
</html>                   

Output: 
 

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

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

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :