May 06, 2021 JavaScript
JavaScript strings are used to store and process text.
Strings can store a series of characters, such as "John Doe."
A string can be any character inserted into quotation marks. You can use single or double quotes:
You can use the index location to access each character in the string:
The index of the string starts at 0, which means that the first character index value is .0, the second is .1, and so on.
You can use quotation marks in strings, and the quotation marks in strings should not be the same as quotation marks in strings:
You can also add escape characters to strings to use quotation marks:
You can use the built-in property length to calculate the length of the string:
Tip: You can learn more about JavaScript's ability to get string lengths in the JavaScript programming battle section of this site.
In JavaScript, strings are written in single or double quotes.
Because of this, the following instance JavaScript cannot be resolved: x
The string "We are the so-called" is truncated.
How to solve the above problems? You can use the backslash to escape the double quotes in the "Vikings" string, as follows:
The backslash is an escape character. Escape characters convert special characters to string characters:
Escape characters () can be used to escape apostrophes, line changes, quotation marks, and other special characters.
The following table lists special characters that can be escaped using escape characters in strings:
Code | Output |
---|---|
\' | Single quotes |
\" | Double quotes |
\\ | Backslash |
\n | Line change |
\r | Enter |
\t | tab (tab) |
\b | The back-checker |
\f | Page break |
Typically, the JavaScript string is the original value and can be created using characters: var firstName s "John"
But we can also use the new keyword to define a string as an object: var firstName s new String ("John").
Do not create String objects. It slows down execution and may have other side effects: |
The original value string, such as "John," has no properties and methods (because they are not objects).
The original value can use JavaScript's properties and methods because JavaScript can execute methods and properties as objects.
The string method we'll cover in the next chapter.
Property | Describe |
---|---|
constructor | Returns the function that created the string property |
length | Returns the length of the string |
prototype | Allows you to add properties and methods to an object |
Method | Describe |
---|---|
charAt() | Returns the character that specifies the location of the index |
charCodeAt() | Returns the Unicode value of the character that specifies the index position |
concat() | Connect two or more strings, returning the connected string |
fromCharCode() | Convert the specified Unicode value to a string |
indexOf() | Returns the location in the string where the specified character appears for the first time |
lastIndexOf() | Returns the location in the string where the specified character last appeared |
localeCompare() | Compare the two strings in a local-specific order |
match() | A match was found for one or more regular expressions |
replace() | Replaces a substruce that matches a regular expression |
search() | Retrieves the value that matches the regular expression |
slice() | Extracts pieces of the string and returns the extracted part in the new string |
split() | Split the string into substring arrays |
substr() | Extracts the specified number of characters from the string from the starting index number |
substring() | Extracts the character between two specified index numbers in the string |
toLocaleLowerCase() | Depending on the host's locale, strings are converted to lesscase, and only a few languages, such as Turkish, have local-specific case maps |
toLocaleUpperCase() | Depending on the host's locale, the string is converted to capital, and only a few languages, such as Turkish, have a local-specific case map |
toLowerCase() | Convert the string to small case |
toString() | Returns the value of the string object |
toUpperCase() | Convert the string to capital |
trim() | Removes the blanks from the beginning and end of the string |
valueOf() | Returns the original value of a string object |
JavaScript Standard Reference Manual: JavaScript String Object