JavaScript: String Overview
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.