The string value must be enclosed in single quotes
( '' ) or double quotes
( "" ).
"My name is John Smith"
'My name is John Smith'
Example: Single quote and Double quote
All above examples are considered to be string.
- String literal
1. Defining String using string literal is easy and preferable. To define using this way just assign text value to the variable in either single quotes or double quotes.
Example: String Using string literal
2. To create a string using String constructor use
new keyword and pass text in the
String function either single quotes or double quotes.
Example: String Using String constructor
One of such properties is
length property. It is used to find the length of a string.
Example: String Length
String can be concatenated using
+ operator between two different strings to concatenate both strings.
Example: String concatenation
The above string will be chopped at "Color of ". And will also cause an error Uncaught SyntaxError: unexpected token: identifier .
To solve the above problem use backslash escape character in the string.
Backslash character (
\ ) is an escape characters which is used to escape the character which has special meaning.
|double quote ( " )||\"||"|
|single quote ( ' )||\'||'|
|backslash ( \ )||\\||\|
Example: String escape character
\' escape single quote.
Example: Single quote escape
To escape backslash itself, write 2 backslashes together (
Example: Escaping backslash