11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Variables<br />

A variable stores data. Every variable has a name, called its identifier. Variables are declared in<br />

<strong>JavaScript</strong> using var, a keyword that allocates storage space for new data and indicates to the<br />

interpreter that a new identifier is in use. Declaring a variable is simple:<br />

var x;<br />

This statement tells the interpreter that a new variable x is about to be used. Variables can be<br />

assigned initial values when they are declared:<br />

var x = 2;<br />

In addition, multiple variables can be declared with one var statement if the variables are<br />

separated by commas:<br />

var x, y = 2, z;<br />

You should not use variables without first declaring them, although it is possible to do so in<br />

certain cases. Using a variable on the right-hand side of an assignment without first declaring it<br />

will result in an error.<br />

Experienced programmers will notice that, unlike C, C++, and Java, there is only one way to<br />

declare a variable in <strong>JavaScript</strong>. This highlights the fact that <strong>JavaScript</strong>‘s treatment of variable<br />

data types is fundamentally different from many languages, including C, C++, and Java.<br />

Basic Data Types<br />

Every variable has a data type that indicates what kind of data the variable holds. <strong>The</strong> basic<br />

data types in <strong>JavaScript</strong> are strings, numbers, and Booleans. A string is a list of characters, and<br />

a string literal is indicated by enclosing the characters in single or double quotes. Strings may<br />

contain a single character or multiple characters, including whitespace and special characters<br />

such as \n (the newline). Numbers are integers or floating-point numerical values, and numeric<br />

literals are specified in the natural way. Booleans take on one of two values: true or false.<br />

Boolean literals are indicated by using true or false directly in the source code. An example of<br />

all three data types follows.<br />

var stringData = "<strong>JavaScript</strong> has strings\n It sure does";<br />

var numericData = 3.14;<br />

var booleanData = true;<br />

<strong>JavaScript</strong> also supports two other basic types: undefined and null. All these data types as well<br />

as the details of special characters are discussed in Chapter 3. However, one aspect of<br />

<strong>JavaScript</strong> data types deserves special mention in this overview—weak typing.<br />

Dynamic Typing<br />

A major difference between <strong>JavaScript</strong> and many other languages readers might be familiar<br />

with is that <strong>JavaScript</strong> is dynamically typed (or, by some definitions, weakly typed). Every<br />

<strong>JavaScript</strong> variable has a data type, but the type is inferred from the variable‘s content. For<br />

example, a variable that is assigned a string value assumes the string data type. A<br />

consequence of <strong>JavaScript</strong>‘s automatic type inference is that a variable‘s type can change<br />

during script execution. For example, a variable can hold a string at one point and then later be<br />

assigned a Boolean. Its type changes according to the data it holds. This explains why there is<br />

only one way to declare variables in <strong>JavaScript</strong>: there is no need to indicate type in variable<br />

declarations.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!