Array Intro
Learn about the basic functionality of arrays.
Last updated
Was this helpful?
Learn about the basic functionality of arrays.
Last updated
Was this helpful?
JavaScript has a built-in object for representing lists of data.
Arrays are just a data structure for holding a collection of items. The syntax looks like this. You can store any type of data in an array.
An array is a list, and items can be accessed by their position in the list relative to the beginning. This is know as their index. Array indices are zero-based, so to access the first element you would specify 0 for the index.
And to access the last element in the list, the index would be the length of the array minus 1.
You can also store objects in an array. For example, you may have a list of users or a list of products in a shopping cart.
You can change an element in the array using the bracket notation and an assignment operator.
An array is an object, and has methods for performing actions on the elements in the array, such as adding and removing elements from the array.
JavaScript arrays are dynamically sized, meaning that it will never run out of room. You can continue to add new items, and the array will grow as needed.
JavaScript arrays have another characteristic that is unusual. It allows different types of data to be stored in the list. For example, you can store an object, and a string, and a number, all in the same array. It's not a common use-case, but is another example of the flexibility of the JavaScript language.