• JavaScript Video Tutorials

JavaScript String length Property



The JavaScript String length (or string.length) property is used to find the number of characters present in the current string.

In JavaScript, a string is a data-type representing a sequence of characters. Strings can consist of letters, numbers, symbols, words, or sentences.

Syntax

Following is the syntax of JavaScript String length property −

string.length

Here, the string refers to the given string whose length you want to find.

Parameters

  • It does not accept any parameter.

Return value

This property returns the length of the string, i.e., the number of characters present in it.

Example 1

If the given string is empty, it will return zero (0) as string length.

In the following program, we are using the JavaScript String length property to find the length of an empty string ("").

<html>
<head>
<title>JavaScript String length Property</title>
</head>
<body>
<script>
   const str = "";
   document.write("The given string: ", str);
   document.write("<br>Length of the given string: ", str.length);
</script>
</body>
</html>

Output

The above program returns the string length as 0.

The given string:
Length of the given string: 0

Example 2

The following is another example of the JavaScript String length property. We use the string.length property to find the number of characters present in the current string 'Tutorials point'.

The given string has 14 characters in it, but it will return the string length as 15 because it considers the white-space between the words as a character.

<html>
<head>
<title>JavaScript String length Property</title>
</head>
<body>
<script>
   const str = "Tutorials point";
   document.write("The given string: ", str);
   document.write("<br>Length of the given string: ", str.length);
</script>
</body>
</html>

Output

After executing the above program, it will return the length of the string as −

The given string: Tutorials point
Length of the given string: 15

Example 3

If the given string is empty but contains white spaces, it will consider all white spaces as characters and return the length of the string.

In the example below, we use the string.length property to retrieve the length of the string " " (which contains only white spaces).

<html>
<head>
<title>JavaScript String length Property</title>
</head>
<body>
<script>
   const str = "    ";
   document.write("The given string: ", str);
   document.write("<br>Length of the given string: ", str.length);
</script>
</body>
</html>

Output

Once the above program is executed, it will return the string length.

The given string:
Length of the given string: 4

Example 4

Let's compare the string length using the string.length property and use the result in a conditional statement to check whether the strings have equal length or not.

<html>
<head>
<title>JavaScript String length Property</title>
</head>
<body>
<script>
   const str1 = "Tutorials Point";
   const str2 = "Hello";
   const str3 = "World";
   document.write("The given strings are : ", str1, ", ", str2, ", ", str3);
   const len1 = str1.length;
   const len2 = str2.length;
   const len3 = str3.length;
   document.write("<br>Length of ", str1, " is: ", len1);
   document.write("<br>Length of ", str2, " is: ", len2);
   document.write("<br>Length of ", str3, " is: ", len3);
   if(len1 == len2){
      document.write("<br>String ", str1, " and ", str2, " having equal length");
   }
   else if(len1 == len3){
      document.write("<br>String ", str1, " and ", str3, " having equal length");
   }
   else if(len2 == len3){
      document.write("<br>String ", str2, " and ", str3, " having equal length");
   }
   else{
      document.write("<br>None of the strings having equal length");
   }
</script>
</body>
</html>

Output

Following is the output of the above program −

The given strings are : Tutorials Point, Hello, World
Length of Tutorials Point is: 15
Length of Hello is: 5
Length of World is: 5
String Hello and World having equal length
Advertisements