JavaScript: Quote String

By Xah Lee. Date: . Last updated: .

Double Quote

Use double straight quote " (U+22: QUOTATION MARK) to enclose string.

console.log( "Mary's cat" );
// Mary's cat

Use \" for double quote in a double quoted string.

Single Quote

Single Quote ' (Use double straight quote ) can also be used.

Use \' for single quote in a single quoted string.

// single quote, with escape
console.log( 'Mary\'s cat' );
// Mary's cat

What's the Difference Between Double and Single Quoted String?

There's basically no difference, except that if you need to include a double/single quote, you need to escape it if it's the same delimiter.

Literal newline in double/single quoted string not allowed

Literal newline in double/single quoted string is not allowed. Use backslash to continue a line.

// illegal syntax. Literal newline is not allowed
let s = "a
b";

// error: The module's source code could not be parsed: Expected ';', '}' or <eof>

use backslash to continue a line

let s = "c\
d";

console.log(s); // prints cd

String Escape Sequence

Use \n for newline.

let ss = "cat\ndog";

console.log(ss);

// prints 2 lines:
// cat
// dog

[see String Escape Sequence]

Template String

template string is a new in 2015. It lets you have literal newline in string, and variable and expressions in string.

JavaScript String

BUY
Ξ£JS
JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Property

Object and Inheritance

Array

Function

Constructor/Class

Iterable 🌟

Misc