JavaScript String Object
String is JavaScript's type for text. Strings are written in quotes or backticks, can't be changed once created, and have methods for searching, slicing, replacing and formatting text.
Description
A String is a sequence of characters used to represent text. Strings can be written with single quotes, double quotes or backticks; backtick strings are template literals, which can span lines and include values with ${expression}. String(value) converts any value to a string.
Strings are immutable: methods such as toUpperCase(), replace() and slice() return a new string and never change the original. Characters are accessed by index with str[0] or str.at(-1), and length counts UTF-16 code units, so some emoji count as 2. To work with whole characters, spread the string into an array: [...str].
Most string methods are case-sensitive, so convert both sides with toLowerCase() for case-insensitive comparisons, or use localeCompare() with the sensitivity option. Comparing strings with < and > uses character codes, which doesn't follow alphabetical order in every language. To repeat, pad, trim, split and join text there are dedicated methods, so there's rarely a need to loop over characters yourself.
Creating
'text'
"text"
`text ${expression}`
String(value)Static methods
| Name | Description |
|---|---|
String.fromCharCode() | The String.fromCharCode() static method creates a string from one or more UTF-16 character codes. |
String.raw() | The String.raw() static method is a template tag that returns a template literal's text without processing escape sequences such as \n. |
Instance properties
| Name | Description |
|---|---|
String.prototype.length | The length property gives the number of characters in a string, counted as UTF-16 code units, so some emoji count as two. |
Instance methods
| Name | Description |
|---|---|
String.prototype.at() | The at() method returns the character at a given index of a string, accepting negative numbers to count back from the end. |
String.prototype.charAt() | The charAt() method returns the character at a given index of a string, or an empty string if there is none. |
String.prototype.charCodeAt() | The charCodeAt() method returns the numeric UTF-16 code of the character at a given index. |
String.prototype.codePointAt() | The codePointAt() method returns the full Unicode code point of the character at a given position, including emoji and other characters beyond the basic range. |
String.prototype.concat() | The concat() method joins strings onto the end of a string and returns the result as a new string. |
String.prototype.endsWith() | The endsWith() method checks whether a string ends with given text, returning true or false. |
String.prototype.includes() | The includes() method checks whether a string contains another string, returning true or false. |
String.prototype.indexOf() | The indexOf() method returns the position of the first occurrence of some text in a string, or -1 if it isn't found. |
String.prototype.lastIndexOf() | The lastIndexOf() method returns the position of the last occurrence of some text in a string, or -1. |
String.prototype.localeCompare() | The localeCompare() method compares two strings according to the rules of a language, for correct alphabetical sorting. |
String.prototype.match() | The match() method searches a string with a regular expression and returns the matches, or null if there are none. |
String.prototype.matchAll() | The matchAll() method returns an iterator of every regular expression match in a string, including capture groups for each one. |
String.prototype.normalize() | The normalize() method converts a string to a standard Unicode form, so text that looks the same compares as equal. |
String.prototype.padEnd() | The padEnd() method pads the end of a string with another string until it reaches a given length. |
String.prototype.padStart() | The padStart() method pads the start of a string with another string until it reaches a given length, such as adding leading zeros. |
String.prototype.repeat() | The repeat() method returns a new string with the original repeated a given number of times. |
String.prototype.replace() | The replace() method returns a new string with the first match of a string or regular expression replaced. |
String.prototype.replaceAll() | The replaceAll() method returns a new string with every occurrence of some text replaced. |
String.prototype.search() | The search() method returns the index of the first match of a regular expression in a string, or -1. |
String.prototype.slice() | The slice() method extracts part of a string between two indexes and returns it as a new string. |
String.prototype.split() | The split() method divides a string into an array of pieces, splitting it wherever a separator appears. |
String.prototype.startsWith() | The startsWith() method checks whether a string begins with given text, returning true or false. |
String.prototype.substring() | The substring() method returns the part of a string between two indexes. |
String.prototype.toLowerCase() | The toLowerCase() method returns a copy of a string with all letters converted to lower case. |
String.prototype.toUpperCase() | The toUpperCase() method returns a copy of a string with all letters converted to upper case. |
String.prototype.trim() | The trim() method removes white space, including spaces, tabs and line breaks, from both ends of a string. |
String.prototype.trimEnd() | The trimEnd() method removes white space from the end of a string only. |
String.prototype.trimStart() | The trimStart() method removes white space from the start of a string only. |
Example
Browser Support
The following information will show you the current browser support for the String object. Hover over a browser icon to see the version that first introduced support for this object.
Desktop





Tablets & Mobile





