String
BaselineWidely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
TheString
object is used to represent and manipulate asequence of characters.
Description
Strings are useful for holding data that can be represented in text form. Some of themost-used operations on strings are to check theirlength
, to build and concatenate them using the+
and+=
string operators,checking for the existence or location of substrings with theindexOf()
method, or extracting substringswith thesubstring()
method.
Creating strings
Strings can be created as primitives, from string literals, or as objects, using theString()
constructor:
const string1 = "A string primitive";const string2 = 'Also a string primitive';const string3 = `Yet another string primitive`;
const string4 = new String("A String object");
String primitives and string objects share many behaviors, but have other important differences and caveats.See "String primitives and String objects" below.
String literals can be specified using single or double quotes, which are treatedidentically, or using the backtick character`. This last form specifies atemplate literal:with this form you can interpolate expressions. For more information on the syntax of string literals, seelexical grammar.
Character access
There are two ways to access an individual character in a string. The first is thecharAt()
method:
"cat".charAt(1); // gives value "a"
The other way is to treat the string as an array-like object, where individual characters correspond to a numerical index:
"cat"[1]; // gives value "a"
When using bracket notation for character access, attempting to delete or assign avalue to these properties will not succeed. The properties involved are neither writablenor configurable. (SeeObject.defineProperty()
for more information.)
Comparing strings
Use theless-than and greater-than operators to compare strings:
const a = "a";const b = "b";if (a < b) { // true console.log(`${a} is less than ${b}`);} else if (a > b) { console.log(`${a} is greater than ${b}`);} else { console.log(`${a} and ${b} are equal.`);}
Note that all comparison operators, including===
and==
, compare strings case-sensitively. A common way to compare strings case-insensitively is to convert both to the same case (upper or lower) before comparing them.
function areEqualCaseInsensitive(str1, str2) { return str1.toUpperCase() === str2.toUpperCase();}
The choice of whether to transform bytoUpperCase()
ortoLowerCase()
is mostly arbitrary, and neither one is fully robust when extending beyond the Latin alphabet. For example, the German lowercase letterß
andss
are both transformed toSS
bytoUpperCase()
, while the Turkish letterı
would be falsely reported as unequal toI
bytoLowerCase()
unless specifically usingtoLocaleLowerCase("tr")
.
const areEqualInUpperCase = (str1, str2) => str1.toUpperCase() === str2.toUpperCase();const areEqualInLowerCase = (str1, str2) => str1.toLowerCase() === str2.toLowerCase();areEqualInUpperCase("ß", "ss"); // true; should be falseareEqualInLowerCase("ı", "I"); // false; should be true
A locale-aware and robust solution for testing case-insensitive equality is to use theIntl.Collator
API or the string'slocaleCompare()
method — they share the same interface — with thesensitivity
option set to"accent"
or"base"
.
const areEqual = (str1, str2, locale = "en-US") => str1.localeCompare(str2, locale, { sensitivity: "accent" }) === 0;areEqual("ß", "ss", "de"); // falseareEqual("ı", "I", "tr"); // true
ThelocaleCompare()
method enables string comparison in a similar fashion asstrcmp()
— it allows sorting strings in a locale-aware manner.
String primitives and String objects
Note that JavaScript distinguishes betweenString
objects andprimitive string values. (The same is true ofBoolean
andNumbers
.)
String literals (denoted by double or single quotes) and strings returned fromString
calls in a non-constructor context (that is, called without usingthenew
keyword) are primitive strings. In contexts where amethod is to be invoked on a primitive string or a property lookup occurs, JavaScriptwill automatically wrap the string primitive and call the method or perform the propertylookup on the wrapper object instead.
const strPrim = "foo"; // A literal is a string primitiveconst strPrim2 = String(1); // Coerced into the string primitive "1"const strPrim3 = String(true); // Coerced into the string primitive "true"const strObj = new String(strPrim); // String with new returns a string wrapper object.console.log(typeof strPrim); // "string"console.log(typeof strPrim2); // "string"console.log(typeof strPrim3); // "string"console.log(typeof strObj); // "object"
Warning:You should rarely find yourself usingString
as a constructor.
String primitives andString
objects also give different results whenusingeval()
. Primitives passed toeval
are treated as source code;String
objects are treated asall other objects are, by returning the object. For example:
const s1 = "2 + 2"; // creates a string primitiveconst s2 = new String("2 + 2"); // creates a String objectconsole.log(eval(s1)); // returns the number 4console.log(eval(s2)); // returns the string "2 + 2"
For these reasons, the code may break when it encountersString
objectswhen it expects a primitive string instead, although generally, authors need not worryabout the distinction.
AString
object can always be converted to its primitive counterpart withthevalueOf()
method.
console.log(eval(s2.valueOf())); // returns the number 4
String coercion
Many built-in operations that expect strings first coerce their arguments to strings (which is largely whyString
objects behave similarly to string primitives).The operation can be summarized as follows:
- Strings are returned as-is.
undefined
turns into"undefined"
.null
turns into"null"
.true
turns into"true"
;false
turns into"false"
.- Numbers are converted with the same algorithm as
toString(10)
. - BigInts are converted with the same algorithm as
toString(10)
. - Symbols throw a
TypeError
. - Objects are firstconverted to a primitive by calling its
[Symbol.toPrimitive]()
(with"string"
as hint),toString()
, andvalueOf()
methods, in that order. The resulting primitive is then converted to a string.
There are several ways to achieve nearly the same effect in JavaScript.
- Template literal:
`${x}`
does exactly the string coercion steps explained above for the embedded expression. - The
String()
function:String(x)
uses the same algorithm to convertx
, except thatSymbols don't throw aTypeError
, but return"Symbol(description)"
, wheredescription
is thedescription of the Symbol. - Using the
+
operator:"" + x
coerces its operand to aprimitive instead of astring, and, for some objects, has entirely different behaviors from normal string coercion. See itsreference page for more details.
Depending on your use case, you may want to use`${x}`
(to mimic built-in behavior) orString(x)
(to handle symbol values without throwing an error), but you should not use"" + x
.
UTF-16 characters, Unicode code points, and grapheme clusters
Strings are represented fundamentally as sequences ofUTF-16 code units. In UTF-16 encoding, every code unit is exact 16 bits long. This means there are a maximum of 216, or 65536 possible characters representable as single UTF-16 code units. This character set is called thebasic multilingual plane (BMP), and includes the most common characters like the Latin, Greek, Cyrillic alphabets, as well as many East Asian characters. Each code unit can be written in a string with\u
followed by exactly four hex digits.
However, the entire Unicode character set is much, much bigger than 65536. The extra characters are stored in UTF-16 assurrogate pairs, which are pairs of 16-bit code units that represent a single character. To avoid ambiguity, the two parts of the pair must be between0xD800
and0xDFFF
, and these code units are not used to encode single-code-unit characters. (More precisely, leading surrogates, also called high-surrogate code units, have values between0xD800
and0xDBFF
, inclusive, while trailing surrogates, also called low-surrogate code units, have values between0xDC00
and0xDFFF
, inclusive.) Each Unicode character, comprised of one or two UTF-16 code units, is also called aUnicode code point. Each Unicode code point can be written in a string with\u{xxxxxx}
wherexxxxxx
represents 1–6 hex digits.
A "lone surrogate" is a 16-bit code unit satisfying one of the descriptions below:
- It is in the range
0xD800
–0xDBFF
, inclusive (i.e., is a leading surrogate), but it is the last code unit in the string, or the next code unit is not a trailing surrogate. - It is in the range
0xDC00
–0xDFFF
, inclusive (i.e., is a trailing surrogate), but it is the first code unit in the string, or the previous code unit is not a leading surrogate.
Lone surrogates do not represent any Unicode character. Although most JavaScript built-in methods handle them correctly because they all work based on UTF-16 code units, lone surrogates are often not valid values when interacting with other systems — for example,encodeURI()
will throw aURIError
for lone surrogates, because URI encoding uses UTF-8 encoding, which does not have any encoding for lone surrogates. Strings not containing any lone surrogates are calledwell-formed strings, and are safe to be used with functions that do not deal with UTF-16 (such asencodeURI()
orTextEncoder
). You can check if a string is well-formed with theisWellFormed()
method, or sanitize lone surrogates with thetoWellFormed()
method.
On top of Unicode characters, there are certain sequences of Unicode characters that should be treated as one visual unit, known as agrapheme cluster. The most common case is emojis: many emojis that have a range of variations are actually formed by multiple emojis, usually joined by the <ZWJ> (U+200D
) character.
You must be careful which level of characters you are iterating on. For example,split("")
will split by UTF-16 code units and will separate surrogate pairs. String indexes also refer to the index of each UTF-16 code unit. On the other hand,[Symbol.iterator]()
iterates by Unicode code points. Iterating through grapheme clusters will require some custom code.
"😄".split(""); // ['\ud83d', '\ude04']; splits into two lone surrogates// "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// The United Nations flag[..."🇺🇳"]; // [ '🇺', '🇳' ]// splits into two "region indicator" letters "U" and "N".// All flag emojis are formed by joining two region indicator letters
Constructor
String()
Creates
String
objects. When called as a function, it returns primitive values of type String.
Static methods
String.fromCharCode()
Returns a string created by using the specified sequence of Unicode values.
String.fromCodePoint()
Returns a string created by using the specified sequence of code points.
String.raw()
Returns a string created from a raw template string.
Instance properties
These properties are defined onString.prototype
and shared by allString
instances.
String.prototype.constructor
The constructor function that created the instance object. For
String
instances, the initial value is theString
constructor.
These properties are own properties of eachString
instance.
length
Reflects the
length
of the string. Read-only.
Instance methods
String.prototype.at()
Returns the character (exactly one UTF-16 code unit) at the specified
index
. Accepts negative integers, which count back from the last string character.String.prototype.charAt()
Returns the character (exactly one UTF-16 code unit) at the specified
index
.String.prototype.charCodeAt()
Returns a number that is the UTF-16 code unit value at the given
index
.String.prototype.codePointAt()
Returns a nonnegative integer Number that is the code point value of the UTF-16encoded code point starting at the specified
pos
.String.prototype.concat()
Combines the text of two (or more) strings and returns a new string.
String.prototype.endsWith()
Determines whether a string ends with the characters of the string
searchString
.String.prototype.includes()
Determines whether the calling string contains
searchString
.String.prototype.indexOf()
Returns the index within this string of the firstoccurrence of
searchValue
, or-1
if not found.String.prototype.isWellFormed()
Returns a boolean indicating whether this string contains anylone surrogates.
String.prototype.lastIndexOf()
Returns the index within this string of the lastoccurrence of
searchValue
, or-1
if not found.String.prototype.localeCompare()
Returns a number indicating whether the reference string
compareString
comes before, after, or is equivalent to thegiven string in sort order.String.prototype.match()
Used to match regular expression
regexp
against a string.String.prototype.matchAll()
Returns an iterator of all
regexp
's matches.String.prototype.normalize()
Returns the Unicode Normalization Form of the calling string value.
String.prototype.padEnd()
Pads the current string from the end with a given string and returns a new string ofthe length
targetLength
.String.prototype.padStart()
Pads the current string from the start with a given string and returns a new stringof the length
targetLength
.String.prototype.repeat()
Returns a string consisting of the elements of the object repeated
count
times.String.prototype.replace()
Used to replace occurrences of
searchFor
usingreplaceWith
.searchFor
may be a stringor Regular Expression, andreplaceWith
may be a string orfunction.String.prototype.replaceAll()
Used to replace all occurrences of
searchFor
usingreplaceWith
.searchFor
may be a stringor Regular Expression, andreplaceWith
may be a string orfunction.String.prototype.search()
Search for a match between a regular expression
regexp
andthe calling string.String.prototype.slice()
Extracts a section of a string and returns a new string.
String.prototype.split()
Returns an array of strings populated by splitting the calling string at occurrencesof the substring
sep
.String.prototype.startsWith()
Determines whether the calling string begins with the characters of string
searchString
.String.prototype.substr()
DeprecatedReturns a portion of the string, starting at the specified index and extending for a given number of characters afterwards.
String.prototype.substring()
Returns a new string containing characters of the calling string from (or between)the specified index (or indices).
String.prototype.toLocaleLowerCase()
The characters within a string are converted to lowercase while respecting thecurrent locale.
For most languages, this will return the same as
toLowerCase()
.String.prototype.toLocaleUpperCase()
The characters within a string are converted to uppercase while respecting thecurrent locale.
For most languages, this will return the same as
toUpperCase()
.String.prototype.toLowerCase()
Returns the calling string value converted to lowercase.
String.prototype.toString()
Returns a string representing the specified object. Overrides the
Object.prototype.toString()
method.String.prototype.toUpperCase()
Returns the calling string value converted to uppercase.
String.prototype.toWellFormed()
Returns a string where alllone surrogates of this string are replaced with the Unicode replacement character U+FFFD.
String.prototype.trim()
Trims whitespace from the beginning and end of the string.
String.prototype.trimEnd()
Trims whitespace from the end of the string.
String.prototype.trimStart()
Trims whitespace from the beginning of the string.
String.prototype.valueOf()
Returns the primitive value of the specified object. Overrides the
Object.prototype.valueOf()
method.String.prototype[Symbol.iterator]()
Returns a new iterator object that iterates over the code points of a String value,returning each code point as a String value.
HTML wrapper methods
Warning:Deprecated. Avoid these methods.
They are of limited use, as they are based on a very old HTML standard and provide only a subset of the currently available HTML tags and attributes. Many of them create deprecated or non-standard markup today. In addition, they do string concatenation without any validation or sanitation, which makes them a potential security threat when directly inserted usinginnerHTML
. UseDOM APIs such asdocument.createElement()
instead.
String.prototype.anchor()
Deprecated<a name="name">
(hypertext target)String.prototype.big()
Deprecated<big>
String.prototype.blink()
Deprecated<blink>
String.prototype.bold()
Deprecated<b>
String.prototype.fixed()
Deprecated<tt>
String.prototype.fontcolor()
DeprecatedString.prototype.fontsize()
DeprecatedString.prototype.italics()
Deprecated<i>
String.prototype.link()
Deprecated<a href="url">
(link to URL)String.prototype.small()
Deprecated<small>
String.prototype.strike()
Deprecated<strike>
String.prototype.sub()
Deprecated<sub>
String.prototype.sup()
Deprecated<sup>
Note that these methods do not check if the string itself contains HTML tags, so it's possible to create invalid HTML:
"</b>".bold(); // <b></b></b>
The only escaping they do is to replace"
in the attribute value (foranchor()
,fontcolor()
,fontsize()
, andlink()
) with"
.
"foo".anchor('"Hello"'); // <a name=""Hello"">foo</a>
Examples
String conversion
TheString()
function is a more reliable way of converting values to strings than calling thetoString()
method of the value, as the former works when used onnull
andundefined
. For example:
// You cannot access properties on null or undefinedconst nullVar = null;nullVar.toString(); // TypeError: Cannot read properties of nullString(nullVar); // "null"const undefinedVar = undefined;undefinedVar.toString(); // TypeError: Cannot read properties of undefinedString(undefinedVar); // "undefined"
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-string-objects |