Length
Lesson 5 of 18 in Coddy's Array Methods in JavaScript course.
The length is not a method but a built-in property of arrays in JavaScript. It represents the number of elements currently present in the array. It's a non-negative integer value that is always one more than the highest valid index within the array.
The primary use case is to determine the number of items in an array. This is crucial for iterating through the array using loops (e.g., for, while), accessing specific elements by index, and performing operations that require knowledge of the array's size. For example :
const fruits = ["apple", "banana", "orange"];
// Accessing array length
const numberOfFruits = fruits.length;
console.log(numberOfFruits); // Output: 3The array "fruits" contains three items. Therefore, when accessing the length of an array, the length will be 3.
Challenge
EasyGiven an array of cupcakes, write JavaScript code to determine and log the total number of cupcakes present.
Try it yourself
const cupcakes = ["Vanilla", "Chocolate", "Red Velvet", "Lemon"];
// Write code here