> Tutorial
Variables are a fundamental concept in every programming language. They are storage locations that keep values in memory so you can reuse them.
Variables can hold several types of values:
12, 5.59"hello"true, falsenull: the absence of a valueTo use a variable, you first need to declare it. You declare it with the var keyword, like this:
You can also give your variable a value at the same time, like this:
To use the value stored in a variable, simply write its name:
In this first AI, line 5 declares the variable enemy, which is initialized with the value getNearestEnemy(), that is, the nearest enemy.
This enemy variable is then used on lines 8* and *11, where its value is passed to the moveToward and useWeapon functions.
Variables declared with the var keyword last for your leek's turn. There is a second type of variable that keeps its value for the whole fight: global variables. To declare a global variable, simply use the global keyword instead of var:
Variables and global variables are used the same way in your code. In the rest of the tutorial, we will mostly use normal variables (sometimes called local variables).
What is a variable?
A value that changes often A location to store a value
Which code declares a variable foo?
var foo = 12 var foo foo variable foo
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.