Introductions
Arrays are used to store multiple values in a single variable. They are one of the most important parts of JavaScript because real-world programs often need to work with lists of names, products, marks, prices, and other data. JavaScript provides many useful array methods for adding, removing, searching, sorting, and transforming data. In this chapter, you will practice common array operations through simple examples. JavaScript Arrays and Array Methods practice questions with solutions help to build concepts.
Question 1: Create and Display an Array
Problem
Create an array containing the names "Rahul", "Aman", and "Priya". Display the complete array.
Solution
let students = ["Rahul", "Aman", "Priya"];
console.log(students);
Output
[ 'Rahul', 'Aman', 'Priya' ]
Step-by-step Explanation
studentsis a variable.- Square brackets
[]are used to create an array. - The array contains three names.
- Each value is separated by a comma.
console.log()displays the complete array.
Question 2: Access an Array Element
Problem
Create an array containing three programming languages and display the second language.
Solution
let languages = ["HTML", "CSS", "JavaScript"];
console.log(languages[1]);
Output
CSS
Step-by-step Explanation
- The array contains three values.
- JavaScript array indexes start from
0. "HTML"is at index0."CSS"is at index1."JavaScript"is at index2.- Therefore,
languages[1]returns"CSS".
Array positions work like this:
HTML CSS JavaScript
0 1 2
Question 3: Find the Length of an Array
Problem
Create an array containing five numbers and find how many elements it contains.
Solution
let numbers = [10, 20, 30, 40, 50];
console.log(numbers.length);
Output
5
Step-by-step Explanation
- The array contains five values.
.lengthreturns the number of elements in the array.- Therefore,
numbers.lengthreturns5.
Question 4: Add an Element Using push()
Problem
Create an array containing three fruits. Add "Mango" to the end of the array using push().
Solution
let fruits = ["Apple", "Banana", "Orange"];
fruits.push("Mango");
console.log(fruits);
Output
[ 'Apple', 'Banana', 'Orange', 'Mango' ]
Step-by-step Explanation
- The array initially contains three fruits.
push()adds a new element to the end."Mango"is added after"Orange".- The array now contains four elements.
Question 5: Remove the Last Element Using pop()
Problem
Create an array of four fruits and remove the last fruit using pop().
Solution
let fruits = ["Apple", "Banana", "Orange", "Mango"];
fruits.pop();
console.log(fruits);
Output
[ 'Apple', 'Banana', 'Orange' ]
Step-by-step Explanation
- The array initially contains four fruits.
pop()removes the last element."Mango"is the last element.- Therefore,
"Mango"is removed. - The array now contains three fruits.
Question 6: Add an Element to the Beginning
Problem
Create an array containing "CSS" and "JavaScript". Add "HTML" to the beginning using unshift().
Solution
let languages = ["CSS", "JavaScript"];
languages.unshift("HTML");
console.log(languages);
Output
[ 'HTML', 'CSS', 'JavaScript' ]
Step-by-step Explanation
- The original array contains two elements.
unshift()adds an element to the beginning."HTML"becomes the first element.- The existing elements move one position forward.
Question 7: Remove the First Element Using shift()
Problem
Create an array of three colors and remove the first color using shift().
Solution
let colors = ["Red", "Green", "Blue"];
colors.shift();
console.log(colors);
Output
[ 'Green', 'Blue' ]
Step-by-step Explanation
- The array starts with
"Red". shift()removes the first element."Red"is removed."Green"becomes the first element."Blue"remains the second element.
Question 8: Find an Element Using includes()
Problem
Create an array of programming languages and check whether "JavaScript" exists in the array.
Solution
let languages = ["Python", "JavaScript", "Java", "C++"];
let result = languages.includes("JavaScript");
console.log(result);
Output
true
Step-by-step Explanation
- The
languagesarray contains several programming languages. includes()checks whether a specific value exists."JavaScript"is present in the array.- Therefore, the result is
true.
If the value does not exist, includes() returns false.
Question 9: Find the Position of an Element
Problem
Create an array containing four fruits and find the index of "Orange" using indexOf().
Solution
let fruits = ["Apple", "Banana", "Orange", "Mango"];
let position = fruits.indexOf("Orange");
console.log(position);
Output
2
Step-by-step Explanation
- Array indexes start from
0. "Apple"is at index0."Banana"is at index1."Orange"is at index2.indexOf()returns the position of the matching element.- Therefore, the output is
2.
Question 10: Loop Through an Array
Problem
Create an array containing four student names and use a for loop to display each name.
Solution
let students = ["Rahul", "Aman", "Priya", "Neha"];
for (let i = 0; i < students.length; i++) {
console.log(students[i]);
}
Output
Rahul
Aman
Priya
Neha
Step-by-step Explanation
- The array contains four names.
i = 0starts the loop at the first index.i < students.lengthkeeps the loop running while valid indexes remain.students[i]accesses the current element.i++moves to the next index.- The loop continues until every student name has been displayed.
The indexes are:
Rahul → 0
Aman → 1
Priya → 2
Neha → 3
Key Takeaways
- Arrays store multiple values in a single variable.
- Arrays are created using square brackets
[]. - Array indexes start from
0. .lengthreturns the number of elements.push()adds an element to the end.pop()removes the last element.unshift()adds an element to the beginning.shift()removes the first element.includes()checks whether an element exists.indexOf()finds the position of an element.- A
forloop can be used to process every array element. - Arrays are commonly used for lists such as names, prices, marks, and products.
FAQs
1. What is an array in JavaScript?
An array is a data structure that allows you to store multiple values in a single variable.
let fruits = ["Apple", "Banana", "Mango"];
2. How do I access an array element?
Use its index number.
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[0]);
Output:
Apple
3. Why does JavaScript array indexing start from 0?
JavaScript uses zero-based indexing, meaning the first element is at index 0, the second is at index 1, and so on.
4. What does push() do?
push() adds one or more elements to the end of an array.
let numbers = [10, 20];
numbers.push(30);
console.log(numbers);
Output:
[10, 20, 30]
5. What does pop() do?
pop() removes the last element from an array.
let numbers = [10, 20, 30];
numbers.pop();
console.log(numbers);
Output:
[10, 20]
6. What is the difference between shift() and unshift()?
shift() removes the first element, while unshift() adds one or more elements to the beginning.
let numbers = [20, 30];
numbers.unshift(10);
console.log(numbers);
Output:
[10, 20, 30]
7. How can I loop through an array?
You can use a for loop.
let numbers = [10, 20, 30];
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
This displays every element of the array.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
