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.

This code declares a function named add that adds its arguments together and ―returns‖ the<br />

resulting value. <strong>The</strong> return statement tells the interpreter what value the function evaluates to.<br />

For example, you can set the value of the function equal to a variable:<br />

var result = add(2, 3);<br />

<strong>The</strong> arguments 2 and 3 are passed to the function, the body of the function executes, and the<br />

result of their addition, 5, is placed in the variable result.<br />

Besides passing in literal values to a function, it is also possible to pass in variables. For<br />

example:<br />

var a = 3, b=5;<br />

var result;<br />

result = add(a,b);<br />

Experienced programmers might ask whether it is possible to modify the values of variables<br />

that are passed in to functions. <strong>The</strong> answer is more a piece of advice: no. <strong>JavaScript</strong> employs<br />

passing by value for primitive data types, so the values of the variables a and b should remain<br />

unchanged regardless of what happens in the function add. However, other data types, notably<br />

objects, can be changed when passed in (they are passed by reference), making the process<br />

confusing to some. If you have programmed in other languages before, you will recognize that<br />

functions are variously called procedures, subroutines, and methods. As you can see,<br />

functions, which are discussed in detail in Chapter 6, are very powerful.<br />

Input and Output in <strong>JavaScript</strong><br />

<strong>The</strong> ability to perform input and output (I/O) is an integral part of most languages. Because<br />

<strong>JavaScript</strong> executes in a host environment like a Web browser, its I/O facilities might be<br />

different from what you would expect. For obvious security reasons, plain client-side <strong>JavaScript</strong><br />

is not usually allowed to read or write files in the local file system. <strong>The</strong>re are exceptions, but<br />

these are considerably more advanced and will not be addressed until a later chapter.<br />

I/O, like most useful tasks in <strong>JavaScript</strong>, is carried out through the objects provided by the<br />

browser. Interacting with the user is typically achieved through the Window object, several<br />

methods of which are described here. One of the most common I/O methods in <strong>JavaScript</strong> is<br />

using the alert() method of Window, which displays its argument message in a dialog box that<br />

includes an OK button. For example,<br />

alert("This is an important message!");<br />

causes the following dialog box to be presented to the user:<br />

Other forms of dialog with the user include the confirm() method, which displays its argument<br />

message in a dialog box with both OK and Cancel buttons. With the script<br />

confirm("Learn <strong>JavaScript</strong>?");<br />

you should see the following window:

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

Saved successfully!

Ooh no, something went wrong!