JavaScript | Array prototype Constructor

The JavaScript array prototype constructor is used to allow to add new methods and properties to the Array() object. If 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.

Program 1: This example use JavaScript array prototype constructor and convert string character into upper case character.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        JavaScript Array prototype Constructor
    </title>
      
    <style
        h1 { 
            border: 1px solid black; 
            height: 80px; 
            padding-top: 35px; 
            background: green; 
            color: white; 
        
    </style>
</head>
  
<body style = "text-align:center;">  
     
    <h1 style = "color:white;" >
        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>

chevron_right


Output:
Before Clicking the button:

After Clicking the button:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
Before Clicking the button:

After Clicking the button:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.