Intro
An Array is an object type designed for storing data collections.
An array is a list of values, known as elements.
Arrays can store elements of different data types (numbers, strings, objects and other arrays).
Example
const cars = ["Saab", "Volvo", "BMW"];
let car = cars[0];
Arrays are Objects
Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays.
But, JavaScript arrays are best described as arrays.
Arrays use numbers to access its "elements". In this example, person[0] returns John:
const person = ["John", "Doe", 46];
Object:
const person = {firstName:"John", lastName:"Doe", age:46};
The length Property
The length property of an array returns the length of an array (the number of array elements).
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let length = fruits.length;
Accessing the Last Array Element
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits[fruits.length - 1];
Top comments (0)