English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript String Object

The JavaScript String object is used to store and process text.

String object

The JavaScript String object is a global object used to store strings.

The string can be any text within double quotes or single quotes:

var msg = "Hello world";
var msg = 'Hello world';

Unlike some other languages, JavaScript does not distinguish between single quotes and double quotes.

The index of JavaScript strings is zero: the first element of the string is located at index 0, the second element is1, and so on.

You can find it in ourIn the JavaScript String tutorialLearn more about String.

String object properties

The following table lists the properties of the String object:

PropertiesDescription
constructorReturn the function that creates the prototype of the String object
lengthReturn the length of the string
prototypeAllows you to add new properties and methods to the object

String object methods

The following table lists the methods of the String object:

MethodDescription
charAt()Return the character at the specified index
charCodeAt()Return the Unicode of the character at the specified index
concat()Concatenate two or more strings and return a new string
endsWith()Check if the string ends with the specified substring
fromCharCode()Convert a Unicode value to a character
includes()Check if the string contains the specified substring
indexOf()Return the index of the first occurrence of the specified value in the string
lastIndexOf()Return the index of the last occurrence of the specified value in the string
localeCompare()Compare two strings in the current language environment
match()Match the string with a regular expression and return an array of all matches
repeat()Return a new string containing a specified number of copies of the original string
replace()Replace the string or pattern that appears in the string with another string and return a new string without modifying the original string
search()Search the string using regular expressions and return the index of the first match
slice()Extract a part of the string and return it as a new string
split()Split the string into an array of substrings
startsWith()Check if the string starts with the specified substring
substr()Extract a portion of the string starting from the index and following characters
substring()Extract the string between the start index and the end index
toLocaleLowerCase()Convert the string to lowercase letters based on the current language environment of the host
toLocaleUpperCase()Convert the string to uppercase letters based on the current language environment of the host
toLowerCase()Convert the string to lowercase letters
toString()Return the value of the String object
toUpperCase()Convert the string to uppercase letters
trim()Remove whitespace from the beginning and end of the string
valueOf()Return the original value of the String object

Note:All string methods return a new value. They do not change the original variable.