Arrays

JS4Scratch Reference Manual:

Array Definition & Usage

An array in Scratch is called a "list". Arrays are a type of variable. Each value in the array is addressed using an "index" which starts at zero not one. So, in the example below, "Monday" is the first value of the array and it's index is zero:

array[index] = value

eg days[0] = "Monday"

To define the entire array at once:

var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

Individual values of the array can also be defined:

days[0] = "Monday"

Array values can be numbers or strings:

var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
var maxTemp = [27, 35, 23, 26, 30, 31, 29]

Array values can be treated just like normal string/number variables but you address them using the index:

console.log('Max Temp on Wed was:')
console.log(maxTemp[2])

Example:

// double slash denotes code comment & is not run
// example array
console.log("This is an example array");
console.log("An array is a type of variable")
// define the array variable
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
console.log("all the days are:")
console.log(days)
console.log("the 3rd day of the week is:")
//the array index starts at 0 not 1 so 3rd day is index 2
console.log(days[2])