JavaScript strings
JavaScript strings are used to store and manipulate text.
JavaScript strings
A string can store a sequence of characters, such as "John Doe".
A string can be any characters inserted inside quotes. You can use single quotes or double quotes:
Example
var carname = "Volvo XC60";
var carname = 'Volvo XC60';
You can use an index position to access each character in a string:
Example
var character = carname[7];
A string's index starts at 0, meaning the first character has index [0], the second [1], and so on.
Example
const
name
=
"RUNOOB"
;
let letter
=
name
[
2
]
;
document.
getElementById
(
"demo"
)
.
innerHTML
=
letter
;
Try it »
You can use quotes inside a string, as long as they are not the same as the quotes delimiting the string:
Example
var answer = "It's alright";
var answer = "He is called 'Johnny'";
var answer = 'He is called "Johnny"';
You can also add an escape character to use quotes inside a string:
Example
var x = 'It's alright';
var y = "He is called "Johnny"";
Try it »
String Length
You can use the built-in property length to calculate the length of a string:
Example
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
Try it »
Special characters
In JavaScript, strings are written inside single or double quotes.
Because of this, JavaScript cannot parse the following example:
"We are the so-called "Vikings" from the north."
The string "We are the so-called " is truncated.
How can you solve the above problem? You can use a backslash (\) to escape the double quotes in the "Vikings" string, as follows:
"We are the so-called "Vikings" from the north."
A backslash is anEscape characterescape character. Escape characters convert special characters into string characters:
The escape character (\) can be used to escape apostrophes, line breaks, quotes, and other special characters.
The following table lists the special characters that can be escaped with escape characters in strings:
| Code | Output |
|---|---|
| ' | Single quote |
| " | Double quote |
| \ | Backslash |
| \n | Newline |
| \r | Carriage return |
| \t | tab (tab character) |
| \b | Backspace |
| \f | Form feed |
Strings can be objects
Usually, JavaScript strings are primitive values, created using characters: var firstName = "John"
But we can also use the new keyword to define a string as an object: var firstName = new String("John")
Example
var x = "John";
var y = new String("John");
typeof x // returns String
typeof y // returns Object
Try it »
![]() |
Do not create String objects. They slow down execution and may produce other side effects: |
|---|
Example
var x = "John";
var y = new String("John");
(x === y) // The result is false because x is a string and y is an object
Try it »
=== means strict equality, where both the data type and the value must be equal.
String properties and methods
Primitive strings, such as "John", have no properties or methods (because they are not objects).
Primitive values can use JavaScript's properties and methods, because JavaScript can treat primitive values as objects when executing methods and properties.
We will introduce string methods in the next chapter.
String properties
| property | Description |
|---|---|
| constructor | Returns the function that created the string's properties |
| length | Return the length of a string |
| prototype | Allows you to add properties and methods to an object. |
String methods
For more method examples, see:JavaScript String object。
| Method | Description |
|---|---|
| charAt() | Returns the character at the specified index |
| charCodeAt() | Returns the Unicode value of the character at the specified index |
| concat() | Concatenates two or more strings and returns the concatenated string |
| fromCharCode() | Converts Unicode to a string |
| indexOf() | Returns the position of the first occurrence of a specified character in a string |
| lastIndexOf() | Returns the position of the last occurrence of a specified character in a string |
| localeCompare() | Compares two strings using a locale-specific order |
| match() | Finds one or more matches of a regular expression |
| replace() | Replaces substrings that match a regular expression |
| search() | Searches for values that match a regular expression |
| slice() | Extracts a section of a string and returns the extracted part in a new string |
| split() | Splits a string into an array of substrings |
| substr() | Extracts a specified number of characters from a string starting at a given index |
| substring() | Extracts the characters between two specified indices in a string |
| toLocaleLowerCase() | Converts a string to lowercase according to the host's locale; only a few languages (such as Turkish) have locale-specific case mappings |
| toLocaleUpperCase() | Converts a string to uppercase according to the host's locale; only a few languages (such as Turkish) have locale-specific case mappings |
| toLowerCase() | Converts a string to lowercase |
| toString() | Returns the value of a String object |
| toUpperCase() | Converts a string to uppercase |
| trim() | Removes leading and trailing whitespace from a string |
| valueOf() | Returns the primitive value of a String object |
