JavaScript Is More Than You Might Think

  • 6/15/2013

What’s in a JavaScript program?

A JavaScript program consists of statements and expressions formed from tokens of various categories, including keywords, literals, separators, operators, and identifiers placed together in an order that is meaningful to a JavaScript interpreter, which is contained in most web browsers. That sentence is a mouthful, but these statements are really not all that complicated to anyone who has programmed in just about any other language. An expression might be:

var smallNumber = 4;

In that expression, a token, or reserved word—var—is followed by other tokens, such as an identifier (smallNumber), an operator (=), and a literal (4). (You learn more about these elements throughout the rest of the book.) The purpose of this expression is to set the variable named smallNumber equal to the integer 4.

Like in any programming language, statements get put together in an order that makes a program perform one or more functions. JavaScript defines functions in its own way, which you read much more about in Chapter 7. JavaScript defines several built-in functions that you can use in your programs.

Using the javascript pseudo-protocol and a function

  1. Open a web browser.

  2. In the address bar, type the following code and press Enter:

    javascript:alert("Hello World");

    After you press Enter, you see a dialog box similar to this one:


Congratulations! You just programmed your first (albeit not very useful) bit of JavaScript code. However, in just this little bit of code, are two important items that you are likely to use in your JavaScript programming endeavors: the javascript pseudo-protocol identifier in a browser and, more importantly, the alert function. You’ll examine these items in more detail in later chapters; for now, it suffices that you learned something that you’ll use in the future!

JavaScript is also event-driven, meaning that it can respond to certain events or “things that happen,” such as a mouse click or text change within a form field. Connecting JavaScript to an event is central to many common uses of JavaScript. In Chapter 11, you see how to respond to events by using JavaScript.