How to get the length of a string in JavaScript?

In this tutorial, we will learn how to get the length of a string in JavaScript.

While working with strings in JavaScript, we often need to know the number of characters present in them. Understanding how to calculate string length helps with text processing, validation, and data manipulation. There are different ways to get the length of a string, but the most common approach is using the built-in length property.

Using the string.length Property

The simplest and most efficient way to get the length of a string in JavaScript is using the length property.

Syntax

string.length

The length property returns the number of characters in the string, including spaces, punctuation, and special characters.

Example

Here's how to use the length property with different types of strings:

<html>
<body>
   <h3>Use of <i>string.length</i> Property</h3>
   <p id="result1"></p>
   <p id="result2"></p>
   <p id="result3"></p>
   <script>
      var str = "Comma, dot. and Apostrophe' are counted in a string";
      document.getElementById("result1").innerHTML = "String: " + str;
      
      var len = str.length;
      document.getElementById("result2").innerHTML = "Length: " + len;
      
      // More examples
      var emptyStr = "";
      var spaceStr = "   ";
      document.getElementById("result3").innerHTML = 
         "Empty string length: " + emptyStr.length + 
         ", Space string length: " + spaceStr.length;
   </script>
</body>
</html>
String: Comma, dot. and Apostrophe' are counted in a string
Length: 52
Empty string length: 0, Space string length: 3

Custom Function to Get String Length

You can also create a custom function to calculate string length. This approach manually counts each character by iterating through the string.

Syntax

function getLength(str) {
   var count = 0;
   while (str[count] !== undefined) {
      count++;
   }
   return count;
}

Example

In this example, we create a custom function to find the string length:

<html>
<body>
   <h4>Custom Function to get length of a string</h4>
   <p id="result1"></p>
   <p id="result2"></p>
   <script>
      let str = "Tutorials Point Simply Easy Learning";
      document.getElementById("result1").innerHTML = "String: " + str;
      
      function getLength(inputStr) {
         var count = 0;
         while (inputStr[count] !== undefined) {
            count++;
         }
         return count;
      }
      
      var customLength = getLength(str);
      document.getElementById("result2").innerHTML = "Custom Length: " + customLength;
   </script>
</body>
</html>
String: Tutorials Point Simply Easy Learning
Custom Length: 35

Comparison of Methods

Method Performance Ease of Use Browser Support
string.length Fast Very Easy All browsers
Custom function Slower More complex All browsers

Key Points

  • The length property counts all characters including spaces, punctuation, and special characters
  • Empty strings have a length of 0
  • String length is read-only and cannot be modified directly
  • Unicode characters are counted as single characters

Conclusion

The string.length property is the standard and most efficient way to get string length in JavaScript. While custom functions work, they're unnecessary since the built-in property provides the same result with better performance.

Updated on: 2026-03-15T23:18:59+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements