< Language Tutorials

Document stub.png This article is a stub. It may be incomplete, unfinished, or have missing parts/sections. If the article can be expanded, please do so! There may be suggestions on its talk page. (August 2020)

This tutorial will discuss arrays in JavaScript and compare them to lists in Scratch.

Creating a List or Array

An example of a list in Scratch

In Scratch, a list can be made using the "Make a List" button in the Variables section of the Block Palette. In JavaScript, an array can be created and stored in a variable with this syntax:

const aNewArray = [item1, item2, item3];

For example, this is the JavaScript version of the list in the image to the right:

const inventory = ['ruby', 'crystal', 'shovel', 'shoes', 'pan', 'emerald'];

Arrays can hold different data types, just like lists in Scratch:

const aNewArray = ['string', 5, false];

Adding Items to a List or Array

In Scratch, an item can be added to a list with the add [thing] to [list v] block. In JavaScript, the push() method can be used to add items to an array. For example, to add "gold" to the "inventory" list in Scratch, you could do this:

when green flag clicked
add [gold] to [inventory v]

In JavaScript, this could be done:

const inventory = ['ruby', 'crystal', 'shovel', 'shoes', 'pan', 'emerald'];
inventory.push('gold');