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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Part II: Core Language<br />

Chapter 3: Data Types and Variables<br />

Chapter 4: Operators, Expressions, and Statements<br />

Chapter 5: Functions<br />

Chapter 6: Objects<br />

Chapter 7: Array, Date, Math, and Type Related Objects<br />

Chapter 8: Regular Expressions<br />

Chapter 3: Data Types and Variables<br />

Although <strong>JavaScript</strong> was primarily intended to be used to manipulate text in the form of HTML<br />

Web pages within a browser, the data types it offers go well beyond what would be required for<br />

the task. Present in <strong>JavaScript</strong> are most—if not all—of the data types you‘d find in other<br />

modern scripting languages, as well as a robust set of features with which to manipulate them.<br />

<strong>The</strong> basic types <strong>JavaScript</strong> supports are numbers, strings, and Booleans. More complex types<br />

such as objects, arrays, and functions are also part of the language. This chapter covers in<br />

detail the basic data types and their usage. Functions and composite types, such as objects,<br />

are also briefly introduced, but a complete exposition of their capabilities is reserved for<br />

Chapters 5 and 6.<br />

Key Concepts<br />

A variable can be thought of as a container that holds data. It‘s called a ―variable‖ because the<br />

data it contains—its value—varies depending on your script. For example, you might place the<br />

total price of items a customer is buying in a variable, and then add tax to this amount, storing<br />

the result back in the variable. <strong>The</strong> type of a variable describes the nature of the data stored.<br />

For example, the type of a variable holding the value 3.14 would be number while the type of a<br />

variable holding a sentence would be string. Note that ―string‖ is programming language lingo<br />

for a sequence of characters—in other words, some text.<br />

Since you need to have some way to refer to variables, each variable is given an identifier, a<br />

name that refers to the container and allows the script to access and manipulate the data it<br />

contains. Not surprisingly, a variable‘s identifier is often referred to as its name. When scripts<br />

are run, the <strong>JavaScript</strong> interpreter (the facility within the browser that executes <strong>JavaScript</strong>)<br />

needs to allocate space in memory to store a variable‘s value. Declaring a variable is the<br />

process of telling the interpreter to get ready to store data in a new variable. In <strong>JavaScript</strong>,<br />

variables are declared using the var keyword with the name of the variable you wish to declare.<br />

For example, you might write<br />

var firstName;<br />

You can now store data in the variable known by the identifier firstName. Presumably, you‘d be<br />

storing a string here. We could then assign a value like "Thomas" to the variable. We call the<br />

string "Thomas" a literal, which describes any data appearing directly in the source code. <strong>The</strong><br />

complete example is now<br />

var firstName;<br />

firstName = "Thomas";

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

Saved successfully!

Ooh no, something went wrong!