Theory
An array is a linear data structure used to store multiple values. Elements are generally accessed using an index. In JavaScript, array indexing starts from 0. Arrays are useful when we need to store and process a collection of related values.
Example
const numbers = [10, 20, 30, 40];
console.log(numbers[0]);
console.log(numbers[2]);
numbers.push(50);
console.log(numbers);
Expected Output
10, 30 and then the updated array [10, 20, 30, 40, 50] are displayed.
Practice
Create an array of five names and print the first, last and middle element.
Interview Questions
- What is an array?
- What is the index of the first array element?
- What is the time complexity of accessing an array element?
- What are the advantages of arrays?