Finding the length of arrays and strings

by danielkwood

HTML

<html>
    <head>
        <title>Finding the length of arrays and strings</title>
        <script src="script.js"></script>
    </head>
        
    <body>

    </body>
</html>

JavaScript

// Create an array
var cars = ["BMW", "Volkswagen", "Toyota", "Audi"];

// Use the length method to find the length of the array
// The length of an array is the number of elements it contains
var numberofcars = cars.length;
console.log(numberofcars);

// Create a string
var password = "oranges";

// Use the length method to find the length of the string
// The length of a string is how many characters it contains
var passwordlength = password.length;
console.log(passwordlength);

// We can also find the length of an individual string element in an array
// by referring to the index of the element
console.log(cars[1].length);