Add method to String class in JavaScript

Last Updated : 8 Sep, 2026

JavaScript allows developers to extend the String class by adding custom methods to String.prototype. These methods then become available on all string instances.

  • Use Object.defineProperty() to define a custom method with specific property descriptors.
  • Use String.prototype or Object.assign() to add methods directly to the String prototype.
  • Use Symbols when a unique method name is required to avoid property-name conflicts.

Approach 1: Using Object.defineProperty() Method

The Object.defineProperty() method defines a new property directly on an object. By applying it to String.prototype, we can add a custom method that is available to all strings.

Syntax:

Object.defineProperty(obj, propertyName, descriptor)

Example: In this example, we define a SumOfLength() method on String.prototype that returns the sum of the lengths of two strings.

JavaScript
// Input strings
let str1 = "GeeksforGeeks";
let str2 = "A Computer Science Portal";

// Display input strings
console.log(str1);
console.log(str2);

// Define custom method
Object.defineProperty(String.prototype, "SumOfLength", {
    value: function (param) {
        return this.length + param.length;
    }
});

// Run custom method
function GFG_Fun() {
    let res = str1.SumOfLength(str2);

    console.log("Total length: " + res);
}

GFG_Fun();

Approach 2: Using String.prototype

A custom method can be added directly to String.prototype. The this keyword inside the method refers to the string on which the method is called.

Syntax:

String.prototype.methodName = function () {
// Method body
};

Example: In this example, we add the SumOfLength() method to String.prototype.

JavaScript
// Input strings
let str1 = "JavaScript";
let str2 = "A Scripting Language for Web";

// Display input strings
console.log(str1);
console.log(str2);

// Define custom method
String.prototype.SumOfLength = function (arg) {
    return this.length + arg.length;
};

// Run custom method
function GFG_Fun() {
    let res = str1.SumOfLength(str2);

    console.log("Total length: " + res);
}

GFG_Fun();

Approach 3: Using ES6 Classes

ES6 classes can be used to organize custom string-related functionality. However, defining a method inside a custom class does not directly add that method to String.prototype. To use it with strings, a wrapper method can be added to the prototype.

Example: In this example, a custom class handles the string operation, while a method on String.prototype creates an instance of the custom class.

JavaScript
// Input strings
let str1 = "Open";
let str2 = "AI";

// Display input strings
console.log(str1);
console.log(str2);

// Define custom class
class CustomStringMethods {
    constructor(str) {
        this.str = str;
    }

    sumOfLength(otherStr) {
        return this.str.length + otherStr.length;
    }
}

// Add wrapper method to String prototype
String.prototype.CustomStringMethods = function () {
    return new CustomStringMethods(this);
};

// Run custom method
function customMethodExample() {
    const customString1 = str1.CustomStringMethods();

    const res = customString1.sumOfLength(str2);

    console.log("Total length: " + res);
}

customMethodExample();

Approach 4: Using ES6 Symbols

Symbols provide unique property keys. A Symbol can be used as the property name for a method on String.prototype, reducing the possibility of conflicts with existing or other custom methods.

Example: In this example, a Symbol is used to add a unique method for calculating the combined length of two strings.

JavaScript
// Input strings
let str1 = "Hello";
let str2 = "World!";

// Display input strings
console.log(str1);
console.log(str2);

// Define a unique Symbol
const sumOfLengthSymbol = Symbol("sumOfLength");

// Define custom method using Symbol
String.prototype[sumOfLengthSymbol] = function (arg) {
    return this.length + arg.length;
};

// Run custom method
function customMethodWithSymbolExample() {
    let res = str1[sumOfLengthSymbol](str2);

    console.log("Total length: " + res);
}

customMethodWithSymbolExample();

Approach 5: Using Object.assign()

The Object.assign() method can add one or more methods to String.prototype. This is useful when multiple custom methods need to be added together.

Example: In this example, we add a reverse() method to String.prototype.

JavaScript
Object.assign(String.prototype, {
    reverse() {
        return this.split("").reverse().join("");
    }
});

console.log("hello".reverse());
Comment