DEV Community

Zouhair Sahtout
Zouhair Sahtout

Posted on

Arrays - JavaScript

In JavaScript, an array is an ordered list of values. Each value is called an element specified by an index:
Image description1- An array can hold values of mixed types.
For example, you can have an array that stores elements with the types number, string, boolean, and null.
2- The size of an array is dynamic and auto-growing.
In other words, you don’t need to specify the array size up front.



Ways of declaring an array
Image description

It does NOT have to be a number, like here we have an expression
An expression produce a value

We can NOT put a statement inside the []. it expect a value (expression).
console.log(friends[friends.length - 1]);


Change or mutate the array

Image description

Even I declared the array with const, I still can make changes, this is because only primitive values, are immutable.
Arrays, they are NOT a primitive values, so we can mutate them.


Array - methods

We are gonna use push(), but there's more actually
Image descriptionThe push() method in JavaScript adds new items to the end of an array.
The push() method returns the new value of the length property of the array object on which you call the method.

Top comments (0)