βœ… NEW PRODUCTS
🚚 Shipping from 12 USD
πŸ’° DEALS OF THE DAY

Arrays

Basics

Arrays are ordered lists of values and can hold values of any type, including numbers, strings, and other arrays.

Please note that using big arrays will significantly increase the size and memory usage of your program, and both are quite limited for MagiScript. If you need more than a few tens of elements, you should use Uint8Array or our database feature (which will be implemented later).

Creating an Array

To create an array you can use the following syntax:

				
					let myArray = [1, 2, 3, "four", true];
				
			

This creates an array calledΒ myArray, which contains the valuesΒ 1,Β 2,Β 3,Β "four", andΒ true.

Accessing Array Elements

You can access elements in an array using the square bracket notation:

				
					let myArray = [1, 2, 3, "four", true];

console.log(myArray[0]); // Output: 1
console.log(myArray[3]); // Output: four
				
			

In the above example, we access the first element of the array usingΒ myArray[0], and the fourth element usingΒ myArray[3].

Length of an Array

Arrays have aΒ lengthΒ property, which returns the number of elements in the array:

				
					let myArray = [1, 2, 3, "four", true];

console.log(myArray.length); // Output: 5
				
			

In the above example, we access theΒ lengthΒ property of theΒ myArrayΒ array usingΒ myArray.length.

Modifying Array Elements

You can modify elements in an array using the square bracket notation as well:

				
					let myArray = [1, 2, 3, "four", true];

myArray[1] = "two";

console.log(myArray[1]); // Output: two
				
			

In the above example, we modify the second element of theΒ myArrayΒ array by setting it toΒ "two"Β usingΒ myArray[1].

Iterating Over the Elements of an Array

You can use aΒ forΒ loop to iterate over every element of an array:

				
					let myArray = [1, 2, 3, "four", true];

for (let index = 0; index < myArray.length; index++) {
  console.log(myArray[index]);
}
				
			

Conclusion

Arrays in MagiScript are similar to arrays in JavaScript. They are created using square brackets, and you can access elements using the square bracket notation. They also have aΒ lengthΒ property, which returns the number of elements in the array. At this time, there are no other methods available for arrays in MagiScript.

Devices & MagiScript

βš›οΈ Atom Remote

πŸ”’ Quantum Calculator

πŸ€Ήβ€β™‚οΈ MagiScript Language

πŸ€Ήβ€β™‚οΈ MagiScript Examples

πŸ€Ήβ€β™‚οΈ MagiScript Tutorials

General

πŸ“ƒ First Steps

MagiScript Editor

πŸ“ƒ Basics

πŸ“ƒ Keyboard Shortcuts

πŸ“ƒ Running Your First Program

πŸ“ƒ App Store

πŸ“ƒ Atom Settings (Editor)

πŸ“ƒ Debugging Techniques

Input/Output

πŸ“ƒ Buttons

πŸ“ƒ Vibration Motor

πŸ“ƒ RGB LED

πŸ“ƒ Devices

πŸ“ƒ PeekSmith

πŸ“ƒ Quantum

πŸ“ƒ Teleport

πŸ“ƒ Spotted Dice

πŸ“ƒ SB Watch

πŸ“ƒ IARVEL Watch

πŸ“ƒ Fossil Watch

πŸ“ƒ Cosmos Printer

πŸ“ƒ PeriPage Printer

πŸ“ƒ ATC Remote

πŸ“ƒ Labco Scrabble

πŸ“ƒ Bluetooth Keyboard

πŸ“ƒ Bluetooth Mouse

πŸ“ƒ Timers

πŸ“ƒ Database

πŸ“ƒ Events

πŸ“ƒ System (exit, sleep, rand)

πŸ“ƒ Objects (card, time)

Language

πŸ“ƒ Summary

πŸ“ƒ Comments

πŸ“ƒ Variable Declarations

πŸ“ƒ Operators

πŸ“ƒ Control Flow

πŸ“ƒ Functions

πŸ“ƒ Numbers

πŸ“ƒ Strings

πŸ“ƒ Arrays

πŸ“ƒ Objects

πŸ“ƒ Uint8Array