JavaScript Strings
Strings are for storing text
Strings are for storing text
Strings are written with quotes
Share
Using Quotes
A JavaScript string is zero or more characters written inside quotes.
Example
Example Code
javascriptlet text = "John Doe";You can use single or double quotes:
Example
Example Code
javascriptlet carName1 = "Volvo XC60";
// Double quotes
let carName2 = 'Volvo XC60'; // Single quotesQuotes Inside Quotes
You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
Example
Example Code
javascriptlet answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';Template Strings
Templates were introduced with ES6 (JavaScript 2016).
Templates are strings enclosed in backticks (This is a template string).
Templates allow single and double quotes inside a string:
Example
Example Code
javascriptlet text = `He's often called "Johnny"`;String Length
To find the length of a string, use the built-in length property:
Example
Example Code
javascriptlet text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;Escape Characters
Because strings must be written within quotes, JavaScript will misunderstand this string:
Example
Example Code
javascriptlet text = "We are the so-called "Vikings" from the north.";The string will be chopped to "We are the so-called ".
To solve this problem, you can use an backslash escape character.
The backslash escape character (\) turns special characters into string characters:
| Code | Result | Description |
|---|---|---|
| \' | ' | Single quote |
| \" | " | Double quote |
| \\ | \ | Backslash |
Examples
Example Code
javascriptlet text = "We are the so-called \"Vikings\" from the north.";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 Lines
For readability, programmers often like to avoid long code lines.
A safe way to break up a statement is after an operator:
Example
Example Code
javascriptdocument.getElementById("demo").innerHTML =
"Hello Dolly!";A safe way to break up a string is by using string addition:
Example
Example Code
javascriptdocument.getElementById("demo").innerHTML = "Hello " +
"Dolly!";Template Strings
Templates were introduced with ES6 (JavaScript 2015).
Templates are strings enclosed in backticks (This is a template string).
Templates allow multiline strings:
Example
Example Code
javascriptlet text =
`The quick
brown fox
jumps over
the lazy dog`;JavaScript Strings as Objects
Normally, JavaScript strings are primitive values, created from literals:
Example
Example Code
javascriptlet x = "John";But strings can also be defined as objects with the keyword new:
Example
Example Code
javascriptlet y = new String("John");Example
Example Code
javascriptlet x = "John";
let y = new String("John");Example
Example Code
javascriptlet x = "John";
let y = new String("John");Example
Example Code
javascriptlet x = "John";
let y = new String("John");Example
Example Code
javascriptlet x = new String("John");
let y = new String("John");Example
Example Code
javascriptlet x = new String("John");
let y = new String("John");Interactive Code Examples
JS Example 1
javascriptlet text = "John Doe";JS Example 2
javascriptlet carName1 = "Volvo XC60";
// Double quotes
let carName2 = 'Volvo XC60'; // Single quotesJS Example 3
javascriptlet answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';JS Example 4
javascriptlet text = `He's often called "Johnny"`;JS Example 5
javascriptlet text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;Example 6
javascriptlet text = "We are the so-called "Vikings" from the north.";