Q1. What is an Array in JavaScript?
An array is a special variable used to store multiple values in a single variable. Each value in an array is called an element, and each element has an index number starting from 0.
Example
Output
Arrays allow storing and managing multiple values easily.
Example
let fruits = ["Apple", "Banana", "Mango"];console.log(fruits[0]);Output
AppleArrays allow storing and managing multiple values easily.
Q2. How Do You Create an Array in JavaScript?
Arrays can be created using two methods:
1. Using Array Literal (Most Common)
2. Using Array Constructor
The array literal method is recommended because it is simpler and faster.
1. Using Array Literal (Most Common)
let numbers = [10, 20, 30];2. Using Array Constructor
let numbers = new Array(10, 20, 30);The array literal method is recommended because it is simpler and faster.
Q3. How Do You Access Elements in an Array?
Array elements are accessed using their index number.
Example
Output
Example
let colors = ["Red", "Green", "Blue"];console.log(colors[1]);Output
GreenQ4. How Can You Find the Length of an Array?
JavaScript provides the length property to find the total number of elements in an array.
Example
Output
Example
let numbers = [1, 2, 3, 4];console.log(numbers.length);Output
4Q5. How Do You Add Elements to an Array?
JavaScript provides methods to add elements.
push() --> Adds element at the end
unshift() --> Adds element at the beginning
push() --> Adds element at the end
let fruits = ["Apple", "Banana"];fruits.push("Mango");unshift() --> Adds element at the beginning
fruits.unshift("Orange");