Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

The JavaScript Language 481

17


Let’s break down a variable declaration into pieces. Here’s a declaration:


var message = "My message";


The line begins with var, which indicates that this is a variable declaration. The name
of this variable is message. There are a number of rules that apply to naming variables,
which I will list shortly. The assignment operator (=) is used to assign a value to the
variable when it’s declared. The value returned by the expression on the right side of the
operator is assigned to the newly declared variable.


Variable names must conform to the following rules:


n Variable names can include only letters, numbers, and the underscore (_) or dollar
sign ($) character.
n Variable names cannot start with a number.
n You cannot use reserved words as a variable name. Reserved words are words that
have a specific meaning for the JavaScript interpreter. For example, naming a vari-
able var won’t work. Table 17.2 contains a full list of JavaScript reserved words.
n As a matter of style, JavaScript variables begin with a lowercase letter. If a variable
name contains multiple words, usually an underscore is used to join the two words,
or the first letter of the second word is uppercase. So you would write my_variable
or myVariable.

TABLE 17.2 JavaScript Reserved Words
abstract final return


arguments finally short


boolean float static


break for super


byte function switch


case goto synchronized


catch if this


char implements throw


class import throws


const in transient


continue instanceof true


debugger int try


default interface typeof


delete let var

Free download pdf