Simple Types

JavaScript is a trim language, with just enough functionality to do the job—no more, no less. However, as I’ve said before, it is a confusing language in some respects.

For instance, there are just three simple data types: string, numeric, and boolean. Each is specifically differentiated by the literal it contains: string, numeric, and boolean. However, there are also built-in objects known as number, string, and boolean. These would seem to be the same thing, but aren’t: the first three are classifications of primitive values, while the latter three are complex constructions with a type of their own: object.

Rather than mix type and object, in the next three sections, we’ll look at each of the simple data types, how they’re created, and how values of one type can be converted to others. In Chapter 4, we’ll look at these and other built-in JS objects, and the methods and properties accessible with each.

The String Data Type

A string variable was demonstrated in Example 2-1. Since JavaScript is a loosely typed language, there isn’t anything to differentiate it from a variable that’s a number or a boolean, other than the literal value assigned it when it’s initialized and the context of the use.

A string literal is a sequence of characters delimited by single or double quotes:

"This is a string"
'But this is also a string'

There is no rule as to which type of quote you use, except that the ending quote character must be the same as the beginning one. ...

Get Learning JavaScript 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.