Skip to content

Instantly share code, notes, and snippets.

@j201
Last active December 21, 2015 01:29
Show Gist options
  • Save j201/6227909 to your computer and use it in GitHub Desktop.
Save j201/6227909 to your computer and use it in GitHub Desktop.
// JavaScript's strings aren't multiline by default
var poem = "old pond . . .
a frog leaps in
water’s sound";
// SyntaxError: unterminated string literal
// You can get multiline strings by escaping the newline...
var poem = "old pond . . . \
a frog leaps in \
water’s sound";
// "old pond . . . a frog leaps in water’s sound"
// But you need \n to represent newlines
var poem = "old pond . . . \n\
a frog leaps in \n\
water’s sound";
/* "old pond . . .
a frog leaps in
water’s sound" */
// And characters after the slash will break it
var poem = "old pond . . . \n\
a frog leaps in \n\
water’s sound";
// SyntaxError: unterminated string literal
// Another way is to use the + concatenation operator, which avoids that problem
// and allows you to align the lines without adding whitespace
var poem = "old pond . . . \n" +
"a frog leaps in \n" +
"water’s sound";
/* "old pond . . .
a frog leaps in
water’s sound" */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment