0
0
Javascriptprogramming~15 mins

Why arrays are needed in Javascript - See It in Action

Choose your learning style9 modes available
Why arrays are needed
πŸ“– Scenario: Imagine you have a list of your favorite fruits and you want to keep them all together so you can easily see and use them.
🎯 Goal: You will create a list of fruits using an array, then add a new fruit, and finally display all the fruits.
πŸ“‹ What You'll Learn
Create an array called fruits with the exact values 'Apple', 'Banana', and 'Cherry'
Create a variable called newFruit and set it to the string 'Orange'
Add the newFruit to the fruits array using the push method
Print the fruits array to show all the fruits
πŸ’‘ Why This Matters
🌍 Real World
Arrays are used everywhere to store lists like shopping items, user names, or messages.
πŸ’Ό Career
Knowing arrays is essential for any programming job because they help manage collections of data efficiently.
Progress0 / 4 steps
1
Create the initial array
Create an array called fruits with these exact values: 'Apple', 'Banana', and 'Cherry'.
Javascript
Need a hint?

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

2
Add a new fruit variable
Create a variable called newFruit and set it to the string 'Orange'.
Javascript
Need a hint?

Use const newFruit = 'Orange'; to create the variable.

3
Add the new fruit to the array
Use the push method on the fruits array to add the newFruit variable.
Javascript
Need a hint?

Use fruits.push(newFruit); to add the new fruit.

4
Display the fruits array
Write console.log(fruits) to print the fruits array and see all the fruits.
Javascript
Need a hint?

Use console.log(fruits); to show the array in the console.