Chapter 2. The Grammar of JavaScript

Learning a programming language is a lot like learning any new language: you need to learn new words and punctuation, as well as master a new set of rules. And just as you need to learn the grammar of French to speak French, you must become familiar with the grammar of JavaScript to program JavaScript. This chapter covers the concepts that all JavaScript programs rely on.

If you’ve had any experience with JavaScript programming, many of these concepts may be old hat, so you might just skim this chapter. But if you’re new to JavaScript, or you’re still not sure about the fundamentals, this chapter introduces you to basic (but crucial) topics.

Statements

A JavaScript statement is a basic programming unit, usually representing a single step in a JavaScript program. Think of a statement as a sentence: Just as you string sentences together to create a paragraph (or a chapter, or a book), you combine statements to create a JavaScript program. In the previous chapter, you saw several examples of statements. For example:

alert('Hello world!');

This single statement opens an alert window with the message “Hello World!” In many cases, a statement is a single line of code. Each statement ends with a semicolon—it’s like a period at the end of a sentence. The semicolon makes it clear that the step is over and that the JavaScript interpreter should move on to the next action. When you’re writing a JavaScript program, you generally type a statement, ...

Get JavaScript & jQuery: The Missing Manual, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.