VOOZH about

URL: https://www.digitalocean.com/community/tutorials/understanding-arrays-in-javascript

โ‡ฑ Understanding Arrays in JavaScript | DigitalOcean


๐Ÿ‘ Understanding Arrays in JavaScript

Introduction

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.

oceans.js
// 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.

oceans.js
// 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];
Output
Pacific

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.

Learn more about our products

Tutorial Series: How To Code in JavaScript

JavaScript is a high-level, object-based, dynamic scripting language popular as a tool for making webpages interactive.

Tutorial Series: Working with Arrays in JavaScript

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.

About the author(s)

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.

Still looking for an answer?

Was this helpful?

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โ€ฆ

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

๐Ÿ‘ Creative Commons
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
  • Deploy on DigitalOcean

    Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and AI-native businesses

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOceanโ€™s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow โ€” whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

ยฉ 2026 DigitalOcean, LLC.Sitemap.
Dark mode is coming soon.