![]() |
VOOZH | about |
An array in JavaScript is a type of global object that is used to store data. Arrays consist of an ordered collection or list containing zero or more data types, and use numbered indices starting from 0 to access specific items.
Arrays are very useful as they store multiple values in a single variable, which can condense and organize our code, making it more readable and maintainable. Arrays can contain any data type, including numbers, strings, and objects.
To demonstrate how arrays can be useful, consider assigning the five oceans of the world to their own variables.
// Assign the five oceans to five variables
const ocean1 = "Pacific";
const ocean2 = "Atlantic";
const ocean3 = "Indian";
const ocean4 = "Arctic";
const ocean5 = "Antarctic";
This method is very verbose, and can quickly become difficult to maintain and keep track of.
Using arrays, we can simplify our data.
// Assign the five oceans
let oceans = [
"Pacific",
"Atlantic",
"Indian",
"Arctic",
"Antarctic",
];
Instead of creating five separate variables, we now have one variable that contains all five elements. We used square brackets โ [] โ to create an array.
To access a specific item, append its index to the variable.
// Print out the first item of the oceans array
oceans[0];
OutputPacific
In this tutorial, we will learn how to create arrays; how they are indexed; how to add, modify, remove, or access items in an array; and how to loop through arrays.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
JavaScript is a high-level, object-based, dynamic scripting language popular as a tool for making webpages interactive.
Browse Series: 37 tutorials
An array in JavaScript is a type of global object used to store data. Arrays can store multiple values in a single variable, which can condense and organize our code. JavaScript provides many built-in methods to work with arrays, including mutator, accessor, and iteration methods.
Browse Series: 4 tutorials
Software engineer and open source creator
Community and Developer Education expert. Former Senior Manager, Community at DigitalOcean. Focused on topics including Ubuntu 22.04, Ubuntu 20.04, Python, Django, and more.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Very informative article. Thanks for sharing it with us. I see that you prefer the LET keyword over the VAR keyword to assign variables. LET is very old school, similar to the infamous LET keyword of the original MSDOS BASIC or GWBASIC language of the early 1980โs IBM PCโs. In javascript it means declare a local variable vs the global variable of VAR.
Thank you so muchโฆ very well explained tutorialโฆ
Thatโs a great article. Inspired me to write a 3 part series over Arrays in JS:
Great article!
Iโve just mentioned you have the following statement:
If you would like the original variable to remain unchanged, use slice() and assign the result to a new variable
let newArray = slice(7, 1);
I think what you really mean is:
let newArray = seaCreatures.slice(-seaCreatures.length + 1);
Otherwise what the use of slice(7, 1)? ๐
Tanya , Nice tutorial . I want you to take a look at this thread and explain me how to find index of a value in a nested array ? https://stackoverflow.com/questions/39929796/how-to-get-indexof-location-of-a-value-in-nested-array-in-javascript/39929973
Thank you
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.