Skip to content
Related Articles

Related Articles

JavaScript String length Property

Improve Article
Save Article
  • Last Updated : 19 Oct, 2022
Improve Article
Save Article

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:

object.length
where the object can either be a string object or an array object

Below is an example of the string length Property.

Example: 

JavaScript




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

Output:

3

Example:

Input : print("GeeksForGeeks".length)
Output :13

Input : print([1, 2, 3, 4, 5, 6, 7, 8, 9].length)
Output :9

Explanation: 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 

Program: 

JavaScript




<script>
    // 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();
</script>

Output:

9
13
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!