DEV Community

vishwa v
vishwa v

Posted on

Javascript(Array)

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];
Enter fullscreen mode Exit fullscreen mode

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];
Enter fullscreen mode Exit fullscreen mode

Object:

const person = {firstName:"John", lastName:"Doe", age:46};
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Accessing the Last Array Element

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits[fruits.length - 1];
Enter fullscreen mode Exit fullscreen mode

Top comments (0)