JavaScript string
A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. Strings in JavaScript are primitive data types and immutable, which means they are unchanging.
JavaScript strings are for storing and manipulating text.
A JavaScript string is zero or more characters written inside quotes.
Example
Edit it yourselflet text = "John Doe";
use single or double quotes
- The following example is the use of string both method single or double quotes
Example
Edit it yourselflet carName1 = "Volvo XC60"; // Double quotes
let carName2 = 'Volvo XC60'; // Single quotes
quotes inside a string
You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
Example
Edit it yourself<script>
let answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';
</script>
String Length
To find the length of a string, use the built-in length property:
Example
Edit it yourselflet text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;
Escape Character
Because strings must be written within quotes, JavaScript will misunderstand this string:
Example
Edit it yourselflet text = "We are the so-called "Vikings" from the north.";
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:
Code | Result | Description |
---|---|---|
\' | ' | Single quote |
\" | " | Double quote |
\\ | \ | Backslash |
The sequence \" inserts a double quote in a string:
Example
Edit it yourselflet text = "We are the so-called \"Vikings\" from the north.";
The sequence \\ inserts a backslash in a string:
Example
Edit it yourselflet text = "The character \\ is called backslash.";
Six other escape sequences are valid in JavaScript:
Code | Result |
---|---|
\b | Backspace |
\f | Form Feed |
\n | New Line |
\r | Carriage Return |
\t | Horizontal Tabulator |
\v | Vertical Tabulator |
Breaking Long Code Lines
For best readability, programmers often like to avoid code lines longer than 80 characters.
Example
Edit it yourselfdocument.getElementById("demo").innerHTML =
"Hello Dolly!";
You can also break up a code line within a text string with a single backslash:
Example
Edit it yourselfdocument.getElementById("demo").innerHTML = "Hello \
Dolly!";
JavaScript Strings as Objects
Normally, JavaScript strings are primitive values, created from literals:
Example
Edit it yourselflet x = "John";
But strings can also be defined as objects with the keyword new:
Example
Edit it yourselflet y = new String("John");
Example
Edit it yourselflet x = "John";
let y = new String("John");
Do not create Strings objects.
The new keyword complicates the code and slows down execution speed.
String objects can produce unexpected results:
When using the == operator, x and y are equal:
Example
Edit it yourselflet x = "John";
let y = new String("John");