0
0
Javascriptprogramming~10 mins

Array length property in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding the Array length Property in JavaScript
πŸ“– Scenario: You are helping a small online store keep track of the items in their shopping cart. Each item is stored in an array. You want to find out how many items are currently in the cart.
🎯 Goal: Build a simple program that creates an array of items, then uses the length property to find out how many items are in the array, and finally prints that number.
πŸ“‹ What You'll Learn
Create an array called cartItems with exactly these items: 'apple', 'banana', 'orange'
Create a variable called numberOfItems that stores the length of the cartItems array
Print the value of numberOfItems to the console
πŸ’‘ Why This Matters
🌍 Real World
Arrays are used everywhere in programming to store lists of things like shopping cart items, user names, or messages.
πŸ’Ό Career
Knowing how to use arrays and their properties like <code>length</code> is a basic skill for any JavaScript developer.
Progress0 / 4 steps
1
Create the cartItems array
Create an array called cartItems with these exact items: 'apple', 'banana', 'orange'
Javascript
Need a hint?

Use square brackets [] to create an array and separate items with commas.

2
Create the numberOfItems variable
Create a variable called numberOfItems and set it to the length of the cartItems array using the length property
Javascript
Need a hint?

Use arrayName.length to get the number of items in an array.

3
Print the number of items
Use console.log to print the value of numberOfItems to the console
Javascript
Need a hint?

Use console.log(variableName) to show the value in the console.

4
Run and check the output
Run the program and check that the output is 3, which is the number of items in the cartItems array
Javascript
Need a hint?

The console should show the number 3 because there are three items in the array.