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.

context, the interpreter would first search the form‘s context, then the document‘s, then the<br />

window‘s, the browser‘s, and eventually the global context.<br />

<strong>The</strong> exact details of how this works comprise <strong>JavaScript</strong>‘s object model, a subject discussed in<br />

later chapters. A comprehensive knowledge of the topic is not really required to program in<br />

<strong>JavaScript</strong>, but helps tremendously in understanding where the objects available to your scripts<br />

come from, and how they are related. It will also go a long way in setting you apart from the<br />

typical <strong>JavaScript</strong> developer!<br />

Summary<br />

<strong>JavaScript</strong> provides five primitive data types: number, string, Boolean, undefined, and null. Of<br />

the five, undefined and null are special types that are not used to store data. Support for<br />

complex types includes the composite types (objects and arrays) and functions. Arrays and<br />

functions are special kinds of objects. Each primitive type is associated with an object that<br />

provides methods useful for manipulating that kind of data. Scoping for variables is static: if a<br />

variable is not found in the execution context in which it is referenced, the interpreter recursively<br />

searches enclosing contexts (as defined in the source code) for its value. Because <strong>JavaScript</strong><br />

is weakly typed, automatic type conversion is performed whenever two unequal data types are<br />

operated upon. This feature is powerful, but can also lead to ambiguities and subtle errors.<br />

Novice <strong>JavaScript</strong> programmers are always encouraged to define variables in a common place<br />

and to keep data types consistent across execution of their scripts. <strong>The</strong> next chapter discusses<br />

how to operate on data values in meaningful ways as well as how to alter program flow.<br />

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

This chapter provides an overview of the basic building blocks of every script: operators,<br />

expressions, and statements. <strong>The</strong> data types introduced in the last chapter are used directly as<br />

literals or within variables in combination with simple operators, such as addition, subtraction,<br />

and so on, to create expressions. An expression is a code fragment that can be evaluated to<br />

some data type the language supports. For example, 2+2 is an expression with the numeric<br />

value 4. Expressions are in turn used to form statements—the most basic unit of script<br />

execution. <strong>The</strong> execution of statements is controlled using conditional logic and loops.<br />

For those readers new to programming, after reading this chapter, simple scripts should start to<br />

make sense. For experienced programmers, there should be no surprises in this chapter,<br />

because <strong>JavaScript</strong> is similar to so many other languages: arithmetic and logical operators are<br />

part of the language, as are traditional imperative flow-control constructs such as if, while, and<br />

switch. Seasoned programmers may only need to skim this chapter.<br />

Statement Basics<br />

A <strong>JavaScript</strong> program is made up of statements. For example, a common statement we saw in<br />

the last chapter is one that assigns a value to a variable. <strong>The</strong> statements here use the keyword<br />

var to define variables and the assignment operator (=) to set values for them.<br />

var x = 5;<br />

var y = 10;<br />

Assignment uses the = operator and places the value on the right-hand side into the variable on<br />

the left. For example,<br />

x = y + 10;<br />

adds 10 to y and places the result (20) in x.<br />

Whitespace

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

Saved successfully!

Ooh no, something went wrong!