JavaScript: The Definitive Guide

Previous Chapter 3 Next
 

3. Variables and Data Types

Contents:
Variables
Numbers
Strings
boolean Values
Functions
Objects
Arrays
Null
Undefined
The Date Object
Data Type Wrapper Objects

This chapter introduces two of the most important concepts of programming languages: variables and data types. A variable is a name associated with a data value; we say that the variable "stores" or "contains" the value. Variables allow us to store and manipulate data in our programs.

Just as fundamental as variables are data types. These, as the name suggests, are the types of data that our programs can manipulate. In Chapter 2, Lexical Structure, we saw that we can include numeric, string, and Boolean literals directly in our programs. This chapter provides more detail about these data types, and also introduces three new ones: functions, objects, and arrays.[1] Later chapters of the book will provide much more detail about functions, objects, and arrays.

[1] Technically, objects and arrays are actually two distinct uses of a single data type. Because they are used in such distinct ways, we will usually consider them as separate types in this book.

3.1 Variables

In Chapter 2, Lexical Structure, we considered JavaScript literals: constant values embedded directly (or literally) into a JavaScript program. A program that operated only on constant, literal values would not be a very interesting one, and so JavaScript (and all programming languages) use variables. Variables are names that have values assigned to them. They provide a way to manipulate values by name. The value associated with a name need not be constant; new values may be assigned to existing names. Since the value associated with a name may vary, the names are called variables. For example, the following line of JavaScript assigns the value 2 to a variable named i.

i = 2;

And the following line adds 3 to i and assigns the result to a new variable sum:

sum = i + 3;

Variable Declaration

Although it is often unnecessary, it is good programming style to declare variables before using them. You do this with the var keyword, like this:

var i;
var sum;

You can also declare multiple variables with the same var keyword:

var i, sum;

And you can combine variable declaration with initial assignment to the variable:

var i = 2;

As mentioned above, however, variable declaration is not usually required. The first time you use a variable that is not already declared, it will automatically be declared. The only time you actually need to declare a variable with var is when declaring a local variable inside a function definition (we haven't introduced functions yet) and that variable name is also in use as a "global" variable outside of the function. If you simply used the variable in the function without declaring it, then JavaScript would assume you meant the global variable declared outside the function, and would not automatically declare a local one within the function.

Untyped Variables

An important difference between JavaScript and languages like Java and C is that JavaScript is untyped. This means, in part, that variables can hold values of any data type, unlike Java and C variables which can only hold one type of data. For example, it is perfectly legal in JavaScript to assign a number to a variable and later assign a string to it:

i = 10;
i = "ten";
In C, C++, or Java, these lines of code would be illegal.

A related implication of the fact that JavaScript is an untyped language is that variable declarations do not have to specify a data type for the variable as they do in C, C++, and Java. In those languages, you declare a variable by specifying the name of the data type it will hold and following that by the variable:

int i;  // a declaration of an integer variable in C, C++, or Java
As we've seen, we just use the var keyword to declare variable in JavaScript, with no need to specify a type:

var i;  // a declaration of an untyped JavaScript variable.
In fact, although it is good programming style to declare variables in JavaScript, it is usually unnecessary, precisely because JavaScript is untyped.

Another feature of JavaScript's lack of typing is that values are conveniently and automatically converted from one type to another. If you attempt to append a number to a string, for example, JavaScript will automatically convert the number to the corresponding string so that it can be appended. We'll see more about data type conversion in Chapter 9, Further Topics in JavaScript.

JavaScript is obviously a simpler language for being untyped. The advantage of typed languages, like C++ and Java, is that they enforce rigorous programming, and therefore make it easier to write, maintain, and reuse long, complex programs. Since most JavaScript programs are shorter "scripts," this rigor is not necessary, and we benefit from the simpler syntax.


Previous Home Next
Reserved Words Book Index Numbers

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
Hosted by uCoz