Open In App

JavaScript String length Property

Last Updated : 20 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The length property of the JavaScript object is used to find out the size of that object. This property is used with many objects like JavaScript string, JavaScript array, etc. In the case of strings, this property returns the number of characters present in the string. In the case of an array, this property returns the number of elements present in the array.

Syntax:

string.length

Below are examples of the string length Property.

Example 1: In the first example, 13 is the number of characters in the string “GeeksForGeeks”, hence the output is 13. And likewise in the second example, the length of the array is 9 and so is the output 

JavaScript




// JavaScript to illustrate length property   
function func() {
 
    // length property for string
    console.log("GFG".length)
}
func();


Output

3

Example 2: This example returns the length of the given string and the array.

JavaScript




// JavaScript to illustrate length property
function func() {
 
    // length property for array
    console.log([1, 2, 3, 4, 5, 6, 7, 8, 9].length);
    // length property for string
    console.log("GeeksForGeeks".length)
}
func();


Output

9
13

Example 3: The example we declare an empty string emptyString. It checks if the length of the string is equal to 0, returning true.

Javascript




let emptyString = '';
 
console.log(emptyString.length === 0);


Output

true

Example 4: In the example, we declare a string str with the value ‘GeeksforGeeks’. It calculates the index of the last character by subtracting 1 from the string’s length. The lastIndex is then printed, which is 13.

Javascript




let str = 'GeeksforGeeks';
 
const lastIndex = str.length - 1;
console.log(lastIndex);


Output

12

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browser:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads