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.
General
๐ First Steps
MagiScript Editor
๐ Basics
๐ Keyboard Shortcuts
๐ Running Your First Program
๐ App Store
๐ Debugging Techniques
Examples
๐ Atom Time
๐๏ธ Time Practice
๐ Atom Drum
๐๏ธ Atom Stack
๐ Atom Square
๐ Atom Level
๐ Atom THMPR
๐ Poker Hands
๐ Keyboard Numeric
๐ Keyboard NOKIA
๐ Keyboard Cursor
๐ Keyboard Media
๐ Keyboard Custom
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
๐ Labko Scrabble
๐ Bluetooth Keyboard
๐ Bluetooth Mouse
๐ Timers
๐ Database
๐ Events
๐ System (exit, sleep, rand)
๐ Objects (card, time)
Language
๐ Summary
๐ Comments
๐ Operators
๐ Control Flow
๐ Functions
๐ Numbers
๐ Strings
๐ Arrays
๐ Objects
๐ Uint8Array