Arrays

JS4eBot 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"

Arrays can be declared the same way as other variables by using the "Global Variables" TAB.

To define the entire array at once:

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:

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
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
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])