JavaScript/Strings
[i]Learn· Numbers and strings

Strings

JavaScript's String type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values (UTF-16 code units). Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on. The length of a String is the number of elements in it. You can create strings using string literals or string objects.

String literals

You can declare strings in source code using either single or double quotes:

'foo'
"bar"

Within a string literal, most characters can be entered literally. The only exceptions are the backslash (\, which starts an escape sequence), the quote character being used to enclose the string, which terminates the string, and the newline character, which is a syntax error if not preceded by a backslash.

More advanced strings can be created using escape sequences:

Hexadecimal escape sequences

The number after \x is interpreted as a hexadecimal number.

"\xA9" // "©"

Unicode escape sequences

The Unicode escape sequences require at least four hexadecimal digits following \u.

"\u00A9" // "©"

Unicode code point escapes

With Unicode code point escapes, any character can be escaped using hexadecimal numbers so that it is possible to use Unicode code points up to 0x10FFFF. With the four-digit Unicode escapes it is often necessary to write the surrogate halves separately to achieve the same result.

See also String.fromCodePoint() or String.prototype.codePointAt().

"\u{2F804}"

// the same with simple Unicode escapes
"\uD87E\uDC04"
Source: Numbers and strings by Mozilla Contributors, licensed under CC-BY-SA-2.5. Retrieved 2026-11-18. Changes: Extracted the section "Strings"; removed the code-block language labels; moved headings up one level; made links absolute; examples are the page's own code blocks, unchanged.