Thursday, September 4, 2025
HomeLanguagesJavascriptJavaScript String prototype Property

JavaScript String prototype Property

The prototype property allows to add new properties and methods to the existing JavaScript object types. There are two examples to describe the JavaScript String prototype property.

Syntax:

object.prototype.name = value

Return Value: It returns a reference to the String.prototype object.

Example 1: This example adds a property salary to the all objects.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript String prototype Property
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1>GeeksForGeeks</h1>
      
    <p id="GFG_UP"></p>
  
    <button onclick="GFG_Fun();">
        Click Here
    </button>
      
    <p id="GFG_DOWN"></p>
      
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
          
        function person(name, age) {
            this.name = name;
            this.age = age;
        }
        var p1 = new person("p1", 24);
        up.innerHTML = "Click on the button to add "
                + "a new property to all objects of"
                + " a given type.< br > " 
                + JSON.stringify(p1);
  
        function GFG_Fun() {
            person.prototype.salary = 1000;
            down.innerHTML = p1.salary;
        }
    </script>
</body>
  
</html> 


Output:

Example 2: This example adds a method info to the all objects.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript String prototype Property
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1>GeeksForGeeks</h1>
  
    <p id="GFG_UP"></p>
  
    <button onclick="GFG_Fun();">
        Click Here
    </button>
      
    <p id="GFG_DOWN"></p>
      
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        function person(name, age) {
            this.name = name;
            this.age = age;
        }
        var p1 = new person("p1", 22);
        up.innerHTML = "Click on the button to add"
                + " a new property to all objects "
                + "of a given type.< br > " 
                + JSON.stringify(p1);
  
        function getData(name, age) {
            return "Information of person <br>Name - '" 
                        + name + "'<br>Age - " + age;
        }
  
        function GFG_Fun() {
            person.prototype.info 
                    = getData(p1.name, p1.age);
                      
            down.innerHTML = p1.info;
        }
    </script>
</body>
  
</html> 


Output:

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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS