Strings API Flashcards
String.prototype[@@iterator]()
The [@@iterator]()
method of String
values implements the iterable protocol and allows strings to be consumed by most syntaxes expecting iterables, such as the spread syntax and for...of
loops. It returns a string iterator object that yields the Unicode code points of the string value as individual strings.
Note: Strings
are iterated by Unicode code points. This means grapheme clusters will be split, but surrogate pairs will be preserved.
Note2: You seldom need to call this method directly. The existence of the @@iterator
method makes strings
iterable, and iterating syntaxes like the for...of
loop automatically call this method to obtain the iterator to loop over.
// "Backhand Index Pointing Right: Dark Skin Tone" [..."👉🏿"]; // ['👉', '🏿'] // splits into the basic "Backhand Index Pointing Right" emoji and // the "Dark skin tone" emoji // "Family: Man, Boy" [..."👨👦"]; // [ '👨', '', '👦' ] // splits into the "Man" and "Boy" emoji, joined by a ZWJ
Syntax
string[Symbol.iterator]()
Parameters
None.
Return value
A new iterable iterator object that yields the Unicode code points of the string value as individual strings.
“String.prototype[@@iterator]() - JavaScript | MDN” (MDN Web Docs). Retrieved January 22, 2024.
String.prototype.charAt()
The charAt()
method of String
values returns a new string consisting of the single UTF-16 code unit at the given index.
Unicode code points range from 0
to 1114111
(0x10FFFF)
. charAt()
always returns a character whose value is less than 65536
, because the higher code points are represented by a pair of 16-bit surrogate pseudo-characters. Therefore, in order to get a full character with value greater than 65535
, it is necessary to retrieve not only charAt(i)
, but also charAt(i + 1)
(as if manipulating a string
with two characters), or to use codePointAt(i)
and String.fromCodePoint()
instead. For information on Unicode, see UTF-16 characters, Unicode code points, and grapheme clusters.
Syntax
charAt(index)
Parameters
index
- Zero-based index of the character to be returned. Converted to an integer — undefined
is converted to 0
.
Return value
A string
representing the character (exactly one UTF-16 code unit) at the specified index
. If index
is out of the range of 0 – str.length - 1
, charAt()
returns an empty string.
“String.prototype.charAt() - JavaScript | MDN” (MDN Web Docs). Retrieved January 23, 2024.
String.prototype.charCodeAt()
The charCodeAt()
method of String
values returns an integer between 0
and 65535
representing the UTF-16 code unit at the given index.
charCodeAt()
always indexes the string as a sequence of UTF-16 code units, so it may return lone surrogates. To get the full Unicode code point at the given index, use String.prototype.codePointAt()
.
Syntax
charCodeAt(index)
Parameters
-
index
- Zero-based index of the character to be returned. Converted to an integer —undefined
is converted to0
.
Return value
An integer between 0
and 65535
representing the UTF-16 code unit value of the character at the specified index. If index
is out of range of 0 – str.length - 1
, charCodeAt()
returns NaN
.
“String.prototype.charCodeAt() - JavaScript | MDN” (MDN Web Docs). Retrieved January 23, 2024.
String.prototype.codePointAt()
The codePointAt()
method of String
values returns a non-negative integer that is the Unicode code point value of the character starting at the given index. Note that the index is still based on UTF-16 code units, not Unicode code points.
Syntax
codePointAt(index)
Parameters
index
Zero-based index of the character to be returned. Converted to an integer — undefined
is converted to 0
.
Return value
A non-negative integer representing the code point value of the character at the given index.
- If
index
is out of the range of0 – str.length - 1
,codePointAt()
returnsundefined
. - If the element at
index
is a UTF-16 leading surrogate, returns the code point of the surrogate pair. - If the element at
index
is a UTF-16 trailing surrogate, returns only the trailing surrogate code unit.
Example
"ABC".codePointAt(0); // 65 "ABC".codePointAt(0).toString(16); // 41 "😍".codePointAt(0); // 128525 "\ud83d\ude0d".codePointAt(0); // 128525 "\ud83d\ude0d".codePointAt(0).toString(16); // 1f60d "😍".codePointAt(1); // 56845 "\ud83d\ude0d".codePointAt(1); // 56845 "\ud83d\ude0d".codePointAt(1).toString(16); // de0d "ABC".codePointAt(42); // undefined
“String.prototype.codePointAt() - JavaScript | MDN” (MDN Web Docs). Retrieved January 23, 2024.
String.prototype.concat()
The concat()
method of String
values, concatenates the string arguments to the calling string and returns a new string.
If the arguments are not of the type string
, they are converted to string values before concatenating.
The concat()
method is very similar to the addition/string concatenation operators (+
, +=
), except that concat()
coerces its arguments directly to strings, while addition coerces its operands to primitives first.
Syntax
concat() concat(str1) concat(str1, str2) concat(str1, str2, /* …, */ strN)
Parametersstr1, …, strN
- One or more strings to concatenate to str.
Return value
A new string containing the combined text of the strings provided.
“String.prototype.concat() - JavaScript | MDN” (MDN Web Docs). Retrieved January 23, 2024.
String.prototype.endsWith()
The endsWith()
method of String
values, lets you determine whether or not a string ends with another string. This method is case-sensitive.
Syntax
endsWith(searchString) endsWith(searchString, endPosition)
Parameters
-
searchString
- The characters to be searched for at the end of str. Cannot be a regex. All values that are not regexes are coerced to strings, so omitting it or passingundefined
causesendsWith()
to search for the string"undefined"
, which is rarely what you want. -
endPosition
Optional - The end position at whichsearchString
is expected to be found (the index ofsearchString
’s last character plus1
). Defaults tostr.length
.
Return valuetrue
if the given characters are found at the end of the string, including when searchString
is an empty string; otherwise, false
.
ExceptionsTypeError
- Thrown if searchString is a regex.
Examples
const str = "To be, or not to be, that is the question."; console.log(str.endsWith("question.")); // true console.log(str.endsWith("to be")); // false console.log(str.endsWith("to be", 19)); // true
“String.prototype.endsWith() - JavaScript | MDN” (MDN Web Docs). Retrieved January 24, 2024.
String.fromCharCode()
The String.fromCharCode()
static method returns a string
created from the specified sequence of UTF-16 code units.
NOTE: Unicode code points range from 0
to 1114111
(0x10FFFF
). charCodeAt()
always returns a value that is less than 65536
, because the higher code points are represented by a pair of 16-bit surrogate pseudo-characters. Therefore, in order to produce a full character with value greater than 65535
, it is necessary to provide two code units (as if manipulating a string with two characters). For information on Unicode, see UTF-16 characters, Unicode code points, and grapheme clusters.
Syntax
String.fromCharCode() String.fromCharCode(num1) String.fromCharCode(num1, num2) String.fromCharCode(num1, num2, /* …, */ numN)
Parameters
-
num1, …, numN
- A number between0
and65535
(0xFFFF
) representing a UTF-16 code unit. Numbers greater than 0xFFFF are truncated to the last 16 bits. No validity checks are performed.
Return value
A string of length N consisting of the N specified UTF-16 code units.
“String.fromCharCode() - JavaScript | MDN” (MDN Web Docs). Retrieved January 24, 2024.
String.fromCodePoint()
The String.fromCodePoint()
static method returns a string
created from the specified sequence of code points.
Syntax
String.fromCodePoint() String.fromCodePoint(num1) String.fromCodePoint(num1, num2) String.fromCodePoint(num1, num2, /* …, */ numN)
Parametersnum1, …, numN
- An integer between 0
and 0x10FFFF
(inclusive) representing a Unicode code point.
Return value
A string created by using the specified sequence of code points.
ExceptionsRangeError
- Thrown if numN
is not an integer, is less than 0
, or is greater than 0x10FFFF
after being converted to a number
.
Examples
String.fromCodePoint(42); // "*" String.fromCodePoint(65, 90); // "AZ" String.fromCodePoint(0x404); // "\u0404" === "Є" String.fromCodePoint(0x2f804); // "\uD87E\uDC04" String.fromCodePoint(194564); // "\uD87E\uDC04" String.fromCodePoint(0x1d306, 0x61, 0x1d307); // "\uD834\uDF06a\uD834\uDF07"
“String.fromCodePoint() - JavaScript | MDN” (MDN Web Docs). Retrieved January 24, 2024.
String.prototype.includes()
The includes()
method of String
values performs a case-sensitive search to determine whether a given string may be found within this string, returning true
or false
as appropriate.
Syntax
includes(searchString) includes(searchString, position)
Parameters
-
searchString
- A string to be searched for within str. Cannot be a regex. All values that are not regexes are coerced to strings, so omitting it or passingundefined
causes includes() to search for the string “undefined”, which is rarely what you want. -
position
Optional - The position within the string at which to begin searching for searchString. (Defaults to0
.)
Return valuetrue
if the search string is found anywhere within the given string, including when searchString
is an empty string; otherwise, false
.
ExceptionsTypeError
- Thrown if searchString is a regex.
Examples
const str = "To be, or not to be, that is the question."; console.log(str.includes("To be")); // true console.log(str.includes("question")); // true console.log(str.includes("nonexistent")); // false console.log(str.includes("To be", 1)); // false console.log(str.includes("TO BE")); // false console.log(str.includes("")); // true
“String.prototype.includes() - JavaScript | MDN” (MDN Web Docs). Retrieved January 24, 2024.
String.prototype.indexOf()
The indexOf()
method of String
values searches this string and returns the index of the first occurrence of the specified substring. It takes an optional starting position and returns the first occurrence of the specified substring at an index greater than or equal to the specified number
.
Syntax
indexOf(searchString) indexOf(searchString, position)
Parameters
searchString
- Substring to search for. All values are coerced to strings, so omitting it or passing undefined causes indexOf()
to search for the string "undefined"
, which is rarely what you want.
position
Optional - The method returns the index of the first occurrence of the specified substring at a position greater than or equal to position, which defaults to 0
. If position is greater than the length
of the calling string
, the method doesn’t search the calling string at all. If position is less than zero, the method behaves as it would if position were 0
:
-
'hello world hello'.indexOf('o', -5)
returns4
— because it causes the method to behave as if the second argument were0
, and the first occurrence of o at a position greater or equal to0
is at position4
. -
'hello world hello'.indexOf('world', 12)
returns-1
— because, while it’s true the substring world occurs at index6
, that position is not greater than or equal to12
. -
'hello world hello'.indexOf('o', 99)
returns-1
— because99
is greater than thelength
ofhello world hello
, which causes the method to not search the string at all.
Return value
- The index of the first occurrence of searchString found, or
-1
if not found.
Examples:
"hello world".indexOf(""); // returns 0 "hello world".indexOf("", 0); // returns 0 "hello world".indexOf("", 3); // returns 3 "hello world".indexOf("", 8); // returns 8
“String.prototype.indexOf() - JavaScript | MDN” (MDN Web Docs). Retrieved January 25, 2024.
String.prototype.isWellFormed()
The isWellFormed()
method of String values returns a boolean
indicating whether this string contains any lone surrogates.
Syntax
isWellFormed()
Parameters
None.
Return value
Returns true
if this string does not contain any lone surrogates, false
otherwise.
Examples
const strings = [ // Lone leading surrogate "ab\uD800", "ab\uD800c", // Lone trailing surrogate "\uDFFFab", "c\uDFFFab", // Well-formed "abc", "ab\uD83D\uDE04c", ]; for (const str of strings) { console.log(str.isWellFormed()); } // Logs: // false // false // false // false // true // true
“String.prototype.isWellFormed() - JavaScript | MDN” (MDN Web Docs). Retrieved January 25, 2024.
String.prototype.localeCompare()
The localeCompare()
method of String
values returns an integer indicating whether the referenceStr
(aka the this
value string) string comes before, after or is equivalent to the compared string.
- Negative when the
referenceStr
occurs beforecompareString
- Positive when the
referenceStr
occurs aftercompareString
- Returns
0
if they are equivalent
Warning: Do not rely on exact return values of -1
or 1
! Negative and positive integer results vary between browsers (as well as between browser versions) because the ECMAScript specification only mandates negative and positive values. Some browsers may return -2
or 2
, or even some other negative or positive value.
Syntax
localeCompare(compareString) localeCompare(compareString, locales) localeCompare(compareString, locales, options)
Parameters
The locales
and options
parameters customize the behavior of the function and let applications specify the language whose formatting conventions should be used.
In implementations that support the Intl.Collator API, these parameters correspond exactly to the Intl.Collator() constructor’s parameters. Implementations without Intl.Collator
support are asked to ignore both parameters, making the comparison result returned entirely implementation-dependent — it’s only required to be consistent.
-
compareString
- The string against which thereferenceStr
is compared. All values are coerced to strings, so omitting it or passingundefined
causeslocaleCompare()
to compare against the string"undefined"
, which is rarely what you want. -
locales
Optional - A string with a BCP 47 language tag, or an array of such strings. Corresponds to the locales parameter of the Intl.Collator() constructor. -
options
Optional - An object adjusting the output format. Corresponds to the options parameter of theIntl.Collator()
constructor.
Return value
A negative number if referenceStr occurs before compareString; positive if the referenceStr occurs after compareString; 0
if they are equivalent.
In implementations with Intl.Collator
, this is equivalent to new Intl.Collator(locales, options).compare(referenceStr, compareString)
.
“String.prototype.localeCompare() - JavaScript | MDN” (MDN Web Docs). Retrieved January 25, 2024.
String.prototype.match()
The match()
method of String
values retrieves the result of matching this string against a regular expression.
Syntax
match(regexp)
Parameters
regexp
- A regular expression object, or any object that has a Symbol.match
method.
- If
regexp
is not aRegExp
object and does not have aSymbol.match
method, it is implicitly converted to aRegExp
by usingnew RegExp(regexp)
. - If you don’t give any parameter and use the
match()
method directly, you will get anArray
with an empty string:[""]
, because this is equivalent tomatch(/(?:)/)
.
Return value
An Array
whose contents depend on the presence or absence of the global (g
) flag, or null
if no matches are found.
- If the
g
flag is used, all results matching the complete regular expression will be returned, but capturing groups are not included. - If the
g
flag is not used, only the first complete match and its related capturing groups are returned. In this case,match()
will return the same result asRegExp.prototype.exec()
(an array with some extra properties).
Examples
const str = "For more information, see Chapter 3.4.5.1"; const re = /see (chapter \d+(\.\d)*)/i; const found = str.match(re); console.log(found); // [ // 'see Chapter 3.4.5.1', // 'Chapter 3.4.5.1', // '.1', // index: 22, // input: 'For more information, see Chapter 3.4.5.1', // groups: undefined // ] // Examples with global (`g`) and ignoreCase (`i`) flags const str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; const regexp = /[A-E]/gi; const matches = str.match(regexp); console.log(matches); // ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
“String.prototype.match() - JavaScript | MDN” (MDN Web Docs). Retrieved January 27, 2024.
String.prototype.matchAll()
The matchAll()
method of String
values returns an iterator of all results matching this string against a regular expression, including capturing groups.
Syntax
matchAll(regexp)
Parameters
regexp
- A regular expression object, or any object that has a Symbol.matchAll
method.
- If
regexp
is not aRegExp
object and does not have aSymbol.matchAll
method, it is implicitly converted to aRegExp
by usingnew RegExp(regexp, 'g')
. - If
regexp
is a regex, then it must have the global (g
) flag set, or aTypeError
is thrown.
Return value
- An iterable iterator object (which is not restartable) of matches or an empty iterator if no matches are found. Each value yielded by the iterator is an array with the same shape as the return value of
RegExp.prototype.exec()
.
Exceptions
-
TypeError
- Thrown if the regexp is a regex that does not have the global (g
) flag set (its flags property does not contain"g"
).
Example
const regexp = /foo[a-z]*/g; const str = "table football, foosball"; const matches = str.matchAll(regexp); for (const match of matches) { console.log( `Found ${match[0]} start=${match.index} end=${ match.index + match[0].length }.`, ); } // Found football start=6 end=14. // Found foosball start=16 end=24. // matches iterator is exhausted after the for...of iteration // Call matchAll again to create a new iterator Array.from(str.matchAll(regexp), (m) => m[0]); // [ "football", "foosball" ]
“String.prototype.matchAll() - JavaScript | MDN” (MDN Web Docs). Retrieved January 27, 2024.
String.prototype.normalize()
The normalize()
method of String
values returns the Unicode Normalization Form of this string.
Unicode assigns a unique numerical value, called a code point, to each character. For example, the code point for "A"
is given as U+0041
. However, sometimes more than one code point, or sequence of code points, can represent the same abstract character — the character "ñ"
for example can be represented by either of:
The single code point U+00F1
.
The code point for “n” (U+006E
) followed by the code point for the combining tilde (U+0303
).
const string1 = "\u00F1"; const string2 = "\u006E\u0303"; console.log(string1); // ñ console.log(string2); // ñ
However, since the code points are different, string comparison will not treat them as equal. And since the number of code points in each version is different, they even have different lengths.
const string1 = "\u00F1"; // ñ const string2 = "\u006E\u0303"; // ñ console.log(string1 === string2); // false console.log(string1.length); // 1 console.log(string2.length); // 2
The normalize()
method helps solve this problem by converting a string into a normalized form common for all sequences of code points that represent the same characters. There are two main normalization forms, one based on canonical equivalence and the other based on compatibility.
let string1 = "\u00F1"; // ñ let string2 = "\u006E\u0303"; // ñ string1 = string1.normalize("NFD"); string2 = string2.normalize("NFD"); console.log(string1 === string2); // true console.log(string1.length); // 2 console.log(string2.length); // 2
Syntax
normalize() normalize(form)
Parameters
form
Optional - One of "NFC"
, "NFD"
, "NFKC"
, or "NFKD"
, specifying the Unicode Normalization Form. If omitted or undefined, "NFC"
is used.
These values have the following meanings:
-
"NFC"
- Canonical Decomposition, followed by Canonical Composition. -
"NFD"
- Canonical Decomposition. -
"NFKC"
- Compatibility Decomposition, followed by Canonical Composition. -
"NFKD"
- Compatibility Decomposition.
Return value
A string containing the Unicode Normalization Form of the given string.
Exceptions
-
RangeError
- Thrown if form isn’t one of the values specified above.
“String.prototype.normalize() - JavaScript | MDN” (MDN Web Docs). Retrieved January 29, 2024.