JavaScript String Reference
This chapter contains a brief overview of the properties and method of the global String object.
The JavaScript String Object
The JavaScript String object is a global object that is used to store strings. A string is a sequence of letters, numbers, special characters and arithmetic values or combination of all.
To learn more about String, please check out the JavaScript strings chapter.
String Properties
The following table lists the standard properties of the String object.
Property | Description |
---|---|
length |
Returns the length of a string. |
prototype |
Allows you to add new properties and methods to an String object. |
Note: Every object in JavaScript has a constructor
property that refers to the constructor function that was used to create the instance of that object.
String Methods
The following table lists the standard methods of the String object.
Method | Description |
---|---|
charAt() |
Returns the character at the specified index. |
charCodeAt() |
Returns the Unicode of the character at the specified index. |
concat() |
Joins two or more strings, and returns a new string. |
endsWith() |
Checks whether a string ends with a specified substring. |
fromCharCode() |
Converts Unicode values to characters. |
includes() |
Checks whether a string contains the specified substring. |
indexOf() |
Returns the index of the first occurrence of the specified value in a string. |
lastIndexOf() |
Returns the index of the last occurrence of the specified value in a string. |
localeCompare() |
Compares two strings in the current locale. |
match() |
Matches a string against a regular expression, and returns an array of all matches. |
repeat() |
Returns a new string which contains the specified number of copies of the original string. |
replace() |
Replaces the occurrences of a string or pattern inside a string with another string, and return a new string without modifying the original string. |
search() |
Searches a string against a regular expression, and returns the index of the first match. |
slice() |
Extracts a portion of a string and returns it as a new string. |
split() |
Splits a string into an array of substrings. |
startsWith() |
Checks whether a string begins with a specified substring. |
substr() |
Extracts the part of a string between the start index and a number of characters after it. |
substring() |
Extracts the part of a string between the start and end indexes. |
toLocaleLowerCase() |
Converts a string to lowercase letters, according to host machine's current locale. |
toLocaleUpperCase() |
Converts a string to uppercase letters, according to host machine's current locale. |
toLowerCase() |
Converts a string to lowercase letters. |
toString() |
Returns a string representing the specified object. |
toUpperCase() |
Converts a string to uppercase letters. |
trim() |
Removes whitespace from both ends of a string. |
valueOf() |
Returns the primitive value of a String object. |