Open In App

JavaScript string.length

Improve
Improve
Like Article
Like
Save
Share
Report

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




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


Output:

13
3
0

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

javascript




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


Output:

10
13
2

Example 3 : In this example, the Array.from() method is used to convert the string “GeeksforGeeks” into an array of individual characters. The length property is then used on the array to obtain the number of elements, which represents the length of the original string.

Javascript




const str = "GeeksforGeeks";
const strArray = Array.from(str);
const length = strArray.length;
console.log(length);


Output

13

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


Last Updated : 18 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads