1.JavaScript String
JavaScript strings are used to store and manipulate text.
A string can store a series of characters, such as "John Doe."
The string can be any character inserted into the quotation marks. You can use single or double quotation marks:
var " Volvo XC60 ";
You can use the index location to access each character in a string:
var character = carname[7];
The index of a 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 a string, and the quotation marks in the string do not match the quotation marks of the string:
var " It ' s alright " ; var " He is called ' Johnny ' " ; var ' He is called "Johnny" ';
You can also add escape characters to the string using quotation marks:
var ' it\ ' s alright ' ; var " He is called \ "johnny\" ";
1.1 String length
You can use the built-in property length to calculate the string lengths:
var " abcdefghijklmnopqrstuvwxyz " ; var sln = txt.length;
1.2 Special characters
In JavaScript, strings are written in single or double quotation marks.
Because of this, the following instance of JavaScript cannot be resolved:
"Vikings" from the North . "
The string "We are the so-called" is truncated.
How to solve the above problems? You can use a backslash (\) to escape the double quotation marks in the "Vikings" string, as follows:
" We are the so-called \ ' Vikings\ ' from the north. "
A backslash is an escape character . The escape character converts a special character to a string character:
The escape character (\) can be used to escape apostrophes, line breaks, quotation marks, and other special characters.
The following table lists special characters that can be escaped with escape characters in a string:
Code |
Output |
\‘ |
Single quotation marks |
\" |
Double quotes |
\\ |
Back slash |
\ n |
Line break |
\ r |
Enter |
\ t |
tab (TAB) |
\b |
Backspace |
\f |
Page break |
1.3 The string can be an object
Typically, the JavaScript string is the original value and can be created with characters: var firstName = "John"
But we can also use the New keyword to define a string as an object: var firstName = new String ("John")
var " John " ; var New String ("John"); typeof // returns a String typeof // return Object
Do not create a String object. It slows down execution speed and may have other side effects:
var " John " ; var New String ("John"// result is false because X is a string and Y is the object
= = = is absolutely equal, that is, the data type and value must be equal.
1.4 String properties and methods
Raw value strings, such as "John", have no properties and methods (because they are not objects).
Raw values can use JavaScript properties and methods, because JavaScript can take raw values as objects when executing methods and properties.
1.4.1 String Property
Properties |
Description |
Constructor |
Returns a function that creates a string property |
Length |
Returns the length of a string |
Prototype |
Allows you to add properties and methods to an object |
1.4.2 String method
JavaScript use (vii)