Skip to Content
JavaScript: The Definitive Guide, Fourth Edition
book

JavaScript: The Definitive Guide, Fourth Edition

by David Flanagan
November 2001
Intermediate to advanced
936 pages
68h 43m
English
O'Reilly Media, Inc.
Content preview from JavaScript: The Definitive Guide, Fourth Edition

Chapter 5. Expressions and Operators

This chapter explains how expressions and operators work in JavaScript. If you are familiar with C, C++, or Java, you’ll notice that the expressions and operators in JavaScript are very similar, and you’ll be able to skim this chapter quickly. If you are not a C, C++, or Java programmer, this chapter tells you everything you need to know about expressions and operators in JavaScript.

Expressions

An expression is a phrase of JavaScript that a JavaScript interpreter can evaluate to produce a value. The simplest expressions are literals or variable names, like these:

1.7                       // A numeric literal
"JavaScript is fun!"      // A string literal
true                      // A boolean literal
null                      // The literal null value
/java/                    // A regular expression literal
{ x:2, y:2 }              // An object literal
[2,3,5,7,11,13,17,19]     // An array literal
function(x){return x*x;}  // A function literal
i                         // The variable i
sum                       // The variable sum

The value of a literal expression is simply the literal value itself. The value of a variable expression is the value that the variable contains or refers to.

These expressions are not particularly interesting. More complex (and interesting) expressions can be created by combining simple expressions. For example, we saw that 1.7 is an expression and i is an expression. The following is also an expression:

i + 1.7

The value of this expression is determined by adding the values of the two simpler expressions. The + in this example is an operator that is used to combine ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

JavaScript: A Beginner's Guide, Fourth Edition, 4th Edition

JavaScript: A Beginner's Guide, Fourth Edition, 4th Edition

John Pollock
JavaScript Cookbook, 3rd Edition

JavaScript Cookbook, 3rd Edition

Adam D. Scott, Matthew MacDonald, Shelley Powers

Publisher Resources

ISBN: 0596000480Supplemental ContentCatalog PageErrata