HTML and CSS Reference
In-Depth Information
Now you can perform arithmetic and comparisons, but what can you
do with them? Comparisons are used extensively in branching and
looping, which we'll examine in the section “Branching and looping,”
but if you compare the same fixed values in your program you'll see the
same results every time. You need to store variable factors in the pro-
gram so you can provide different results according to different start-
ing conditions: you need variables.
A variable is a place to store the result of a calculation. If you remem-
ber any of your high school algebra, the concept of a variable ought to
be somewhat familiar. You may remember algebra problems something
like this:
5 = 2 + x
In math, working out the variable's value results in the answer. In this
case, it's clear that x = 3. But in JavaScript, you express it like this:
var x = 5 - 2;
This code is saying “Create a storage space called x; calculate the result
of 5 - 2; store the result in the storage space called x. The var keyword
allocates a variable.”
After you've stored a value in a vari-
able, you can use it in another calcu-
lation, as shown here. First a value is
assigned to x and then to y , and then
the variables x and y are used in a
calculation that assigns a value to z .
The console.log(z) statement is an
example of calling a method on an
object. For now, you don't need to
know what that means (you'll learn
more in the section “Functions and
objects”); just be aware that it prints
Search WWH ::

Custom Search