You can use single or double quotes:
You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
The length of a string is found with the built-in
length property :
The string will be chopped to "We are the so-called ".
The solution to avoid this problem, is to use the backslash escape character.
The backslash (
\) escape character turns special characters into string characters:
\" inserts a double quote in a string:
\' inserts a single quote in a string:
\\ inserts a backslash in a string:
The 6 escape characters above were originally designed to control typewriters, teletypes, and fax machines. They do not make any sense in HTML.
For best readability, programmers often like to avoid code lines longer than 80 characters.
You can also break up a code line within a text string with a single backslash:
\ method is not the preferred method. It might not have universal support.
Some browsers do not allow spaces behind the
A safer way to break up a string, is to use string addition:
You cannot break up a code line with a backslash:
var firstName = "John";
But strings can also be defined as objects with the keyword
var firstName = new String("John");
Don't create strings as objects. It slows down execution speed.
new keyword complicates the code. This can produce some unexpected results:
When using the
== operator, equal strings are equal:
When using the
=== operator, equal strings are not equal, because the
=== operator expects equality in both type and value.
Or even worse. Objects cannot be compared:
Note the difference between (x==y) and (x===y).