Strings in JavaScript

The next basic data type is the string. Strings are used to represent text. They are written by enclosing their content in quotes.

‘Down on the sea’

“Lie on the ocean”

‘Float on the ocean’

You can use single quotes, double quotes, or backticks to mark strings, as long as the quotes at the start and the end of the string match.

Almost anything can be put between quotes, and JavaScript will make a string value out of it. But a few characters are more difficult. You can imag­ine how putting quotes between quotes might be hard. Newlines (the charac­ters you get when you press enter) can be included without escaping only when the string is quoted with backticks (‘ ).

To make it possible to include such characters in a string, the follow­ing notation is used: whenever a backslash (\) is found inside quoted text, it indicates that the character after it has a special meaning. This is called escaping the character. A quote that is preceded by a backslash will not end the string but be part of it. When an n character occurs after a backslash, it is interpreted as a newline. Similarly, a t after a backslash means a tab charac­ter. Take the following string:

“This is the first line\nAnd this is the second”

The actual text contained is this:

This is the first line And this is the second

There are, of course, situations where you want a backslash in a string to be just a backslash, not a special code. If two backslashes follow each other, they will collapse together, and only one will be left in the resulting string value. This is how the string “A newline character is written like “\n”.” can be expressed:

“A newline character is written like \”\\n\”.

Strings, too, have to be modeled as a series of bits to be able to exist inside the computer. The way JavaScript does this is based on the Unicode standard. This standard assigns a number to virtually every character you would ever need, including characters from Greek, Arabic, Japanese, Arme­nian, and so on. If we have a number for every character, a string can be described by a sequence of numbers.

And that’s what JavaScript does. But there’s a complication: JavaScript’s representation uses 16 bits per string element, which can describe up to 216 different characters. But Unicode defines more characters than that—about twice as many, at this point. So some characters, such as many emoji, take up two “character positions” in JavaScript strings. We’ll come back to this in “Strings and Character Codes” on page 92.

Strings cannot be divided, multiplied, or subtracted, but the + operator can be used on them. It does not add, but it concatenates—it glues two strings together. The following line will produce the string “concatenate”:

con” + “cat” + “e” + “nate”

String values have a number of associated functions (methods) that can be used to perform other operations on them. I’ll say more about these in “Methods” on page 62.

Strings written with single or double quotes behave very much the same—the only difference is in which type of quote you need to escape inside of them. Backtick-quoted strings, usually called template literals, can do a few more tricks. Apart from being able to span lines, they can also embed other values.

‘half of 100 is ${100 / 2}’

When you write something inside ${} in a template literal, its result will be computed, converted to a string, and included at that position. The example produces half of 100 is 50.

Source: Haverbeke Marijn (2018), Eloquent JavaScript: A Modern Introduction to Programming,

No Starch Press; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *