.length
Published Aug 8, 2023
Contribute to Docs
The .length
property is used on arrays and strings to quickly find out how many items are in an array or how many characters are in a string. This property is straightforward to use and plays a crucial role in JavaScript, making it simple for developers to work with the data.
Syntax
myString.length
Note: The
.length
property is not a method but a property. It cannot be invoked with parentheses, like.length()
. Instead, it can only be referenced as.length
.
Example
The .length
property is applied to an array and a string, respectively, to quickly determine their sizes (number of elements for an array and number of characters for a string). This property provides a convenient and efficient way to work with data in JavaScript.
const fruits = ['apple', 'banana', 'orange', 'grape'];const numberOfFruits = fruits.length;console.log(numberOfFruits);const message = 'Hello, world!';const messageLength = message.length;console.log(messageLength);
This example results in the following output:
413
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours