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)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay