Finding the length of arrays and strings

Coding with JavaScript

🕑 This lesson will take about 10 minutes

In this lesson, you will learn how to find the length of an array (how many elements are in an array), the length of an element in an array, and the length of a string (how many characters in a string). This can be quite useful in programming, for example, if you wanted to:

  • find out how many items a player has in the inventory in a game (checking the length of an inventory array)

  • find out how many products are in a user’s shopping cart in an online shopping app (checking the length of a shopping cart array)

  • check that a user’s password is long enough for increased security (checking length of a password stored as a string)

Watch the video below and scroll down to see the sample code.

Sample HTML & JavaScript code: