Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript string.length

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The string.length is a property in JavaScript which is used to find the length of a given string. The string.length property returns 0 if the string is empty.

Syntax:

string.length

Parameter: It does not accept any parameter. 

Return Values: It returns the length of the given string.

JavaScript code to show the working of string.length property:

Example 1: This example returns the length of the strings using the string.length property of Javascript.

javascript




<script>
    // Taking some strings
    var x = 'geeksforgeeks';
    var y = 'gfg';
    var z = '';
      
    // Returning the length of the string.
    console.log(x.length);
    console.log(y.length);
    console.log(z.length);
</script>

Output:

13
3
0

Example 2: This example returns the length of the strings using the string.length property of Javascript.

javascript




<script>
    // Taking some strings.
    var x = '2341312134';
    var y = '@#$%^&**((*&^';
      
    // Variable z contains two spaces
    var z = ' ';
      
    // Returning the length of the string.
    console.log(x.length);
    console.log(y.length);
    console.log(z.length);
</script>

Output:

10
13
2

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

Supported Browser:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 3 and above
  • Opera 3 and above
  • Safari 1 and above

My Personal Notes arrow_drop_up
Last Updated : 04 Jan, 2023
Like Article
Save Article
Similar Reads