2
Also called a grave accent mark.
const
nan
=
Number
.
NaN
;
// the same as NaN
const
inf
=
Number
.
POSITIVE_INFINITY
;
// the same as Infinity
We’ll discuss the importance of these values in
Strings
A string is simply text data (the word string comes from “string of characters”—a
word originally used in the late 1800s by typesetters, and then later by mathemati‐
cians, to represent a sequence of symbols in a definite order).
Strings in JavaScript represent Unicode text. Unicode is a computing industry stan‐
dard for representing text data, and includes code points for every character or symbol
in most known human languages (including “languages” that might surprise you,
such as Emoji). While Unicode itself is capable of representing text in any language,
that does not mean that the software rendering the Unicode will be capable of render‐
ing every code point correctly. In this book, we’ll stick to fairly common Unicode
characters that are most likely available in your browser and console. If you are work‐
ing with exotic characters or languages, you’ll want to do additional research on Uni‐
code to understand how code points are rendered.
In JavaScript, string literals are represented with single quotes, double quotes, or
backticks.
The backtick was introduced in ES6 to enable template strings, which we
will cover shortly.
Escaping
When you’re trying to represent text data in a program that’s made up of text data,
the problem is always distinguishing text data from the program itself. Setting off
strings within quotes is a start, but what if you want to use quotes in a string? To solve
this problem, there needs to be a method of escaping characters so they are not taken
as string termination. Consider the following examples (which do not require escap‐
ing):
const
dialog
=
'Sam looked up, and said "hello, old friend!", as Max walked in.'
;
const
imperative
=
"Don't do that!"
;
In
dialog
, we can use double quotes without fear because our string is set off with
single quotes. Likewise, in
imperative
, we can use an apostrophe because the string
is set off with double quotes. But what if we needed to use both? Consider:
// this will produce an error
const dialog = "Sam looked up and said "don't do that!" to Max.";
40 | Chapter 3: Literals, Variables, Constants, and Data Types