Chapter 8. The Boolean, Number, and Math Objects

At this point you've covered fewer than half of the core objects you will need to know about to be confident in JavaScript. Now you're going to spend some time looking at some of the other global objects that you haven't covered, specifically the Boolean object (a vehicle for true and false values), the Number object, and the Math object.

Like the String object, the Boolean and Number objects are at the same time primitives as well as objects. They are simultaneously a value as well as a collection of tools for dealing with those data types. As with strings, the distinction is subtle and rarely comes up in practice. However, if you're doing any complex work with these objects, it's worth knowing the difference. Later I'll be talking about the Math object too, which is useful for working with numbers. If you're not confident with math concepts, don't worry — the Math object is nothing to fear. It's mainly a basic collection of static utility functions and mathematical constants found in most programming languages. You'll be surprised how often you end up using Math, usually to do something simple like round off decimals to the nearest whole number, or choose the highest value between a set of values.

The Boolean Object

One of the simplest data types is the primitive boolean value, which can be equal one of two values: true, or false. Whether you know it or not, you're constantly working with boolean values. Whenever you use an if statement, ...

Get JavaScript® Programmer's Reference 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.