You can now store text or numbers in the variable "myVariable".
If you know what you want to store in the variable when you create it it's possible to assign the value or text string immediately, like this:
var myVariable="Hello world!"
Now there is only one more thing you need to know (right now) about variables, the scope. It's important that you have read the tutorial about functions to understand this explanation.
A variable that has been declared inside a function can't be seen by other functions, it's "local".
A variable that has been declared outside all functions can be seen by all functions and is called a "global" variable.
Let's try to store a variable in a "real" script:
var aCoolVariable="Hello world!"
Keep in mind that spaces and special characters can't be used in variable names, instead most people use capital letters to separate words, like "myMegaVariable".
If you feel like experimenting you can try this:
- Change the text in the message.
- Use numbers instead of text.
- See what happens if you add numbers to a string or vice versa (like 788+"100")
There will be more tutorials on this subject...
Back to tutorial index <<