JavaScript: String Overview

By Xah Lee. Date: . Last updated: .

What is String

String is a sequence of characters. (technically, it is sequence of String Code Unit )

Quoting String

String Escape Sequence

String Operations

String Type

String is one of the JavaScript Value Types

String is a Primitive Value

String is Immutable

JavaScript string is immutable. That means, any time you join string or get a substring or replace parts of the string, a new string is created. All JavaScript string functions return a new string.

In practice, this means, if you need to create a long string, such as repeatedly append to a string one thousand times, you should not use a loop with append to do that. Because, the process of creating a long string is expensive. (The workaround in this case is to use a array. Push string to the array, then use Array.prototype.join to convert it to string.)

String is 16 Bits Unit Sequence

Each β€œcharacter” in string is a 16 bits unit, not a β€œcharacter”. String index is counting these units, not character. This is critical when the string contains Unicode character such as emoji πŸ˜ƒ, where such character takes 2 indexes.

[see JavaScript: String Code Unit]

String Constructor

String value can also be created by using the constructor function String.

[see String Constructor]

String is Iterable

That means, you can use for-of Loop and it will go thru the string by char, not 16bits units.

you can also use Spread Operator to turn a string into a array of chars.

JavaScript String

BUY
Ξ£JS
JavaScript in Depth