How to define a new method for calculating the actual length of string in JavaScript using prototype ?
Last Updated :
14 Oct, 2022
In JavaScript, you can use the String. length property to get the length of a string variable. This method will give you a string that is enclosed in quotes no matter what it is. We will get one issue while using this String. length method. Let's discuss when we will get the issue with that String.length method and how to solve it.
Syntax:
string.length
Here, a string could be a variable with a string value or a direct string written in quotes (" ").
Example 1:
JavaScript
<script>
// Declaring the String with value => "Hello Geeks"
let str1 = "Hello Geeks";
// Printing the length of given string str on console
console.log(str1.length);
</script>
Output:
11
Explanation: String str1 has a value of "Hello Geeks" and has a length of 11. We can directly get the 11 by counting it on our own. And remember length method counts the length when it will encounter any character even if it is an encounter with space, it will count it.
Example 2:
JavaScript
<script>
// Declaring the String with value => " Hello Geeks "
let str2 = " Hello Geeks ";
// Printing the length of given string str on console
console.log(str2.length);
</script>
Output:
15
Explanation: String str2 has a value of " Hello Geeks " and has a length of 15 calculated by the length method. But the actual length of string str2 is 11 only, so because the length method calculates the length of character which is enclosed in quotes no matter what it is.
But we need actual length so how we can achieve it? We can achieve it by defining the prototype for a String object. In the definition, we will define the implementation of this operation.
So let's see the implementation of the new truelength method define by using the prototype.
Variable "geeks" is a String object and that object contains the length method which gives the space counted length so we will modify the length method and of name truelength by defining the prototype of the string object. trim() method of String object will return the string by removing the white spaces from the start and end of the string. We will get the trimmed string then we can calculate the length of the given string and that length of the string will be the actual string length without start and end spaces.
JavaScript
<script>
// Declaring the String with value => " Hello Geeks ".
let geeks = " Hello Geeks ";
// Defining the prototype for the string object.
String.prototype.truelength = function(){
// trim() method for removing the white
// spaces from the start of the
// string as well as end of the string.
console.log(this.trim().length);
}
// calling the truelength method which we
// defined by the prototype.
geeks.truelength();
</script>
Output:
11
Similar Reads
How to Get the Longest String in an Array using JavaScript? Given an array, the task is to get the longest string from the array in JavaScript. Here are a few of the most used techniques discussed with the help of JavaScript. In this article, we will use JavaScript methods to find out the longest string in the array. All approaches are described below with e
5 min read
Can we Change the Length of a JS String using Length Property? The length property of a string returns the number of characters in the string, including spaces, punctuation, and special characters. It is a read-only property, which means that you cannot assign a new value to length directly to change the string's size. The given below example shows that you can
1 min read
How to Get the Length of a String in Bytes in JavaScript ? In JavaScript, determining the length of a string in characters is straightforward using the length property. However, in many cases, particularly when dealing with file sizes, network protocols, or database storage, you might need to know the length of a string in bytes. This is because characters
3 min read
JavaScript - How to Find the First and Last Character of a String? Here are the various methods to find the first and last character of a string in JavaScript.1. Using charAt() MethodThe charAt() method retrieves the character at a specified index.JavaScriptconst s = "JavaScript"; const first = s.charAt(0); const last = s.charAt(s.length - 1); console.log(first); c
2 min read
How to create dynamic length array with numbers and sum the numbers using JavaScript ? In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store a list of elements and access them by a single variable. Unlike most languages where the array is a reference to the multiple variables, in JavaScript array is a single variable
2 min read