Go to: Aardvark download page.

JavaScript

If you want to store a value or some text in your script you need a "container". In JavaScript these containers are called "variables".

You probably remember things like A=2 from school, it works the same way in JavaScript. The only difference is that you have to "declare" a variable before you use it, like this:

var myVariable

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=267

...or...

var myVariable="Hello world!"

Remember that text must be within quotes! That's the only way for JavaScript to tell the difference between numbers and letters!

267 and "267" is NOT the same thing! You can use 267 in equations but JavaScript will see "267" as plain text.

If you have stored the string "Hello world!" in a variable is it possible to later store a number? Yes, JavaScript is not picky and will accept a different type of variable.

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:

<script type="text/javascript">
<!--
var aCoolVariable="Hello world!"
alert(aCoolVariable)
// -->
</script>

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 <<