📖 Scenario: Imagine you run a small bookstore. You want to keep track of the books you have on the shelf. You start by writing down the titles one by one on separate pieces of paper. But soon, it becomes hard to find a book or add new ones quickly.To solve this, you decide to use a list where all book titles are kept together in order. This list is called an array in programming.
🎯 Goal: You will create an array to hold book titles, add a few books, and then print the list. This will show how arrays help organize and access multiple items easily.
📋 What You'll Learn
Create an array called books with three book titles: 'The Hobbit', '1984', and 'Pride and Prejudice'
Create a variable called newBook and set it to 'To Kill a Mockingbird'
Add the newBook to the books array using the push method
Print the books array to show all book titles
💡 Why This Matters
🌍 Real World
Arrays are used everywhere in software to keep lists of things like names, products, messages, or any group of items together.
💼 Career
Understanding arrays is essential for programming jobs because they are the basic way to organize and work with multiple pieces of data efficiently.
Progress0 / 4 steps
1
Create the initial array of books
Create an array called books with these exact book titles: 'The Hobbit', '1984', and 'Pride and Prejudice'
DSA Javascript
Hint
Use square brackets [] to create an array and separate items with commas.
2
Add a new book variable
Create a variable called newBook and set it to the string 'To Kill a Mockingbird'
DSA Javascript
Hint
Use const to create a variable and assign the book title as a string.
3
Add the new book to the array
Use the push method on the books array to add the newBook variable
DSA Javascript
Hint
Call books.push(newBook) to add the new book at the end of the array.
4
Print the list of books
Write console.log(books) to print the books array showing all book titles
DSA Javascript
Hint
Use console.log(books) to display the array in the console.
Practice
(1/5)
1. Why do arrays exist in programming languages like JavaScript?
easy
A. To store multiple values in a single variable in an ordered way
B. To perform mathematical calculations faster
C. To replace all other data structures
D. To store only one value at a time
Solution
Step 1: Understand the purpose of arrays
Arrays allow storing many values together instead of separate variables for each value.
Step 2: Recognize the order and grouping feature
Arrays keep values in order and allow easy access by position (index).
Final Answer:
To store multiple values in a single variable in an ordered way -> Option A
Quick Check:
Arrays group values in order = A [OK]
Hint: Arrays group many values in order inside one variable [OK]
Common Mistakes:
Thinking arrays store only one value
Confusing arrays with math operations
Believing arrays replace all data structures
2. Which of the following is the correct way to create an empty array in JavaScript?
easy
A. let arr = {}
B. let arr = ()
C. let arr = []
D. let arr = ''
Solution
Step 1: Recall JavaScript array syntax
Arrays are created using square brackets [].
Step 2: Identify the correct empty array syntax
Using [] creates an empty array, while {} creates an object, () is invalid, and '' is a string.
Final Answer:
let arr = [] -> Option C
Quick Check:
Empty array uses [] = C [OK]
Hint: Use square brackets [] to create arrays in JavaScript [OK]