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.

myArray[0]="Thomas";<br />

delete myArray[0];<br />

At its heart, <strong>JavaScript</strong> is an object-based language, and everything is derived from the various<br />

objects provided by the language or the browser. For example, <strong>JavaScript</strong> provides objects<br />

corresponding to the primitive data types, such as String, Number, and Boolean, which have<br />

methods to operate upon the respective kinds of data. More complex data-related objects, such<br />

as Array, Math, and Date, are also provided, as are browser-oriented objects such as<br />

Navigator and History and the powerful Document object. <strong>The</strong>re is even a generic Object<br />

that we can use to build our own objects. Details about the process of creating and using<br />

objects require significant explanation that can be found in Chapter 6.<br />

Note <strong>The</strong> instances of objects are typically written all lowercase, while the corresponding object<br />

type is written with an initial capital. Do not worry about this distinction for the time<br />

being—it is discussed in depth in Chapters 6 and 7.<br />

Expressions<br />

Expressions are an important part of <strong>JavaScript</strong> and are the building blocks of many <strong>JavaScript</strong><br />

statements. Expressions are groups of tokens that can be evaluated; for example,<br />

var x = 3 + 3;<br />

is an assignment statement that takes the expression 3 + 3 and puts its value in the variable x.<br />

Literals and variables are the simplest kinds of expressions and can be used with operators to<br />

create more complex expressions.<br />

Operators<br />

Basic operators include familiar arithmetic symbols: = (assignment), + (addition), – (subtraction<br />

or unary negation), * (multiplication), / (division), and % (modulus); all are used here.<br />

var x=3, y=6;<br />

x = -x;<br />

x = y + 2;<br />

x = y – 1;<br />

x = y * y;<br />

x = y / x;<br />

x = y % 4;<br />

In this example, x is first assigned –3, then 8, then 5, then 36, then 2, and finally 2 once again.<br />

Most likely the only unfamiliar operator is modulus (%), which results in the remainder of an<br />

integer division.<br />

<strong>JavaScript</strong> also provides bitwise operators, such as & (AND), | (OR), ^ (NOT), ~ (Exclusive<br />

OR), > (right shift). While bitwise operators will seem familiar to some<br />

C programmers, given the high-level nature of <strong>JavaScript</strong> when it is used within the context of<br />

Web pages, they may seem a little out of place.

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

Saved successfully!

Ooh no, something went wrong!