๐ŸŽ TENYO 2026
๐Ÿšš Shipping from 12 USD
๐Ÿ’ฐ DEALS OF THE DAY

Variable Declarations

About Variables

Variables are an essential part of any programming language, and MagiScript is no exception. In programming, a variable is a container that stores a value, which can be accessed and manipulated by the program. A variable can hold different types of values, such as numbers, strings, and booleans, or it can contain more complex structures as well.

Variable declarations allow you to create a new variable and optionally assign a value to it. There are three ways to declare a variable in MagiScript, using the keywords var, let, and const.

Let

The let declaration is a newer way to declare variables in JavaScript. It has block scope. This means that variables declared with let are only accessible within the block (a block is from a curly bracket until a curly bracket) where they are declared, such as a loop or an if statement.

Here’s an example of how to declare a variable with let:

				
					let name = "Alice";
				
			

In this example, we’ve declared a variable called name and assigned it a value of "Alice". We can use the name variable within the block where it was declared.

Please check this example to understand how it works. It will print “Bob”, then “Alice”, because the variable name belongs to the main function, but overridden by a variable with the same name inside the if’s block. If you were remove the redeclaration from line 4, it would print “Alice” two times.

				
					function main() { 
  let name = 'Alice'; 
  if (1 === 1) { 
    let name = 'Bob'; 
    console.log(name);
  }
  console.log(name);
}
				
			

Const

const is also a newer way to declare variables in JavaScript and has block scope. However, once a value is assigned to a const variable, it cannot be reassigned. This makes const useful for declaring constants in your code.

Here’s an example of how to declare a variable with const:

				
					const PI = 3.14;
				
			

In this example, we’ve declared a constant called PI and assigned it a value of 3.14. We can use the PI constant throughout our code, but we cannot reassign its value.

It is a bit more difficult to understand when the value of the constant variable is an array or object. While you cannot assign an other array or object to the variable, you can modify the contents. In the following code, I’m going to add a new key to the object:

				
					function main() { 
  const name = { 
    first: 'John' 
  }; 
  name.family = 'Doe'; 
  console.log(name.family); 
}
				
			

Var

Var is the oldest way to declare variables in JavaScript. While it is supported by MagiScript, it can be easily misused, so we don’t recommend using it. Variables declared with var have function scope or global scope, depending on where they are declared. This means that they are accessible within the function where they are declared, or globally if they are declared outside of a function. It can go wrong if you are overriding your variable or start using before the var keyword.

Here’s an example of how to declare a variable with var:

				
					var age = 30;
				
			

In this example, we’ve declared a variable called age and assigned it a value of 30. We can now use the age variable throughout our code.

Conclusion

In conclusion, variables are containers that store values. Variable declarations allow you to create new variables and assign values to them. var, let, and const are the three ways to declare variables in MagiScript, each with their own scope and rules.

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