JavaScript String Reference Manual
- Previous Page JS Set
- Next Page JS Objects
JavaScript String
JavaScript strings store a series of characters, such as "Bill Gates".
Strings can be any text within double or single quotes:
let carName1 = "Volvo XC60"; let carName2 = 'Volvo XC60';
String indices (subscripts) start from zero:
The first character is at position 0, the second at 1, and so on.
For knowledge about strings, please visit our JavaScript string tutorial.
String properties and methods
Usually, strings like 'Bill Gates' cannot have methods or properties because they are not objects.
But for JavaScript, methods and properties can also be used for strings, because JavaScript treats strings as objects when executing methods and properties.
JavaScript string methods and properties
Methods | Description |
---|---|
at() | Returns the character at the specified index in the string. |
charAt() | Returns the character at the specified index (position) in the string. |
charCodeAt() | Returns the Unicode value of the character at the specified index in the string. |
codePointAt() | Returns the Unicode value at the specified index (position) in the string. |
concat() | Returns two or more concatenated strings. |
constructor | Returns the constructor of the string. |
endsWith() | Returns whether the string ends with the specified value. |
fromCharCode() | Returns the Unicode value as a character. |
includes() | Returns whether the string contains the specified value. |
indexOf() | Returns the index (position) of the first occurrence of the specified value in the string. |
lastIndexOf() | Returns the index (position) of the last occurrence of the specified value in the string. |
length | Returns the length of the string. |
localeCompare() | Compares two strings in the current locale. |
match() | Searches for a value or regular expression in the string and returns the match. |
padEnd() | Fills characters at the end of the string. |
padStart() | Fills characters from the beginning of the string. |
prototype | Allows you to add properties and methods to an object. |
repeat() | Returns a new string containing the specified number of string copies. |
replace() | Searches for a pattern in the string and returns the string with the first match replaced. |
replaceAll() | Searches for a pattern in the string and returns a new string with all matches replaced. |
search() | Searches for a value or regular expression in the string and returns the index (position) of the match. |
slice() | Extracts a part of the string and returns a new string. |
split() | Splits the string into an array of substrings. |
startsWith() | Checks if the string starts with a specified character. |
substr() | Extracts a specified number of characters starting from a specified index (position) in the string. |
substring() | Extracts the characters between two specified indices (positions) in the string. |
toLocaleLowerCase() | Converts the string to lowercase letters using the host's locale settings and returns it. |
toLocaleUpperCase() | Converts the string to uppercase letters using the host's locale settings and returns it. |
toLowerCase() | Returns the string converted to lowercase letters. |
toString() | Returns the string or string object as a string. |
toUpperCase() | Returns the string converted to uppercase letters. |
trim() | Returns the string with spaces removed. |
trimEnd() | Returns the string with trailing spaces removed. |
trimStart() | Returns the string with leading spaces removed. |
valueOf() | Returns the original value of the string or string object. |
Tip:All string methods return a new value. They do not change the original variable.
String HTML Wrapper Methods
HTML wrapper methods return strings wrapped in HTML tags.
These are not standard methods and may not work as expected.
Methods | Description |
---|---|
anchor() | Display the string as an anchor. |
big() | Display the string in large font. |
blink() | Display the string blinking. |
bold() | Display the string in bold. |
fixed() | Display the string with a fixed spacing font. |
fontcolor() | Display the string with a specified color. |
fontsize() | Display the string with a specified font size. |
italics() | Display the string in italics. |
link() | Display the string as a hyperlink. |
small() | Display the string in small font. |
strike() | Display the string with a line through it. |
sub() | Display the string as subscript text. |
sup() | Display the string as superscript text. |
String object description
Strings are a basic data type in JavaScript. The String class provides methods for operating on the original string values.
The methods of the String object length PropertyDeclared the number of characters in the string.
The String class defines a large number of methods for operating on strings, such as extracting characters or substrings from a string, or retrieving characters or substrings.
Note:JavaScript strings are immutable (immutable), and the methods defined in the String class cannot change the content of the string. Like String.toUpperCase() Such methods return a new string rather than modifying the original string.
In earlier implementations of JavaScript based on the Netscape code base (such as the Firefox implementation), strings behave like read-only character arrays. For example, to extract the third character from a string s, you can use s[2] instead of the more standard s.charAt(2). Additionally, when applying a for/in loop to a string, it will enumerate the array index of each character in the string (but note that the ECMAScript standard stipulates that the length property cannot be enumerated). Because the array behavior of strings is not standardized, it should be avoided.
Supplementary Reading
For more information, please read the relevant content in the JavaScript Advanced Tutorial:
- ECMAScript Reference Types
- Reference types are usually called classes (class) or objects. This section explains the predefined reference types of ECMAScript.
- Previous Page JS Set
- Next Page JS Objects