Search⌘ K
AI Features

Discussion: The Grocery List

Explore how JavaScript arrays store elements and additional properties without affecting the array's length. Understand why properties added to arrays do not count as elements and cannot be iterated by array methods, helping you write clearer, bug-free code.

Verifying the output

Now, it’s time to execute the code and observe the output.

Javascript (babel-node-es2024)
const groceryList = [];
groceryList[0] = "Bread";
groceryList[1] = "Milk";
groceryList.user = "John";
console.log(groceryList.length);

Understanding the output

In this puzzle, the array initially starts as an empty array, like an empty bucket. Then, we start filling up the array. We put the string Bread in the first position of ...