JavaScript Array prototype Constructor

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

  • Example:
    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="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>

    chevron_right

    
    

  • 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.

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="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:

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:

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

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

full-stack-img




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.