JavaScript String length Property
Last Updated :
20 Jul, 2023
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
function func() {
console.log( "GFG" .length)
}
func();
|
Example 2: This example returns the length of the given string and the array.
JavaScript
function func() {
console.log([1, 2, 3, 4, 5, 6, 7, 8, 9].length);
console.log( "GeeksForGeeks" .length)
}
func();
|
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);
|
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);
|
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
Share your thoughts in the comments
Please Login to comment...