Java Reference
In-Depth Information
Variables
Variables are common in programming languages. They are a way of storing a value in
memory for later use. In JavaScript, we start by declaring a variable. This is done using the
keyword var :
var a; // declare a variable called a
<< undefined
var message;
<< undefined
Notice that the console outputs undefined . This is a special JavaScript primitive value
that is covered later in the chapter, but it's basically saying that the variable has been created
but is yet to be assigned a value.
You don't actually have to declare variables before using them, but as we'll see later, bad
things can happen if you choose not to. So remember, a ninja will always declare variables.
You can even declare multiple variables at once:
var a,b,c; // 3 variables declared at once
<< undefined
Note: Rules for Naming Variables
When naming variables, you should try to give them sensible names that de-
scribe what the variable represents; hence, answer is a better variable name
than x .
A variable name can start with any upper or lower case letter, an underscore
(_), or dollar symbol ($). It can also contain numbers but cannot start with
them. Here are some examples:
$name
_answer
Search WWH ::




Custom Search