6

Which browsers support multi-line strings?

"foo \
bar"

As usual, my main suspect for not supporting it is IE. Which IE version is the first that supports it?

Community
  • 1
  • 1
ripper234
  • 222,824
  • 274
  • 634
  • 905
  • 7
    What browsers don't support it? – PeeHaa Dec 10 '12 at 19:48
  • 2
    According to the last comment on the post you linked to, this capability is defined in ECMA-262 5th Edition. From there I guess wikipedia is a good reference - http://en.wikipedia.org/wiki/ECMAScript#Dialects. – Lix Dec 10 '12 at 19:54
  • Specifically, when did IE start supporting it? Do IE 7/8 support it? – ripper234 Dec 11 '12 at 09:19

2 Answers2

13

All current versions of the major browsers accept multi-line strings.

Note: this technique is apparently not in compliance with browser standards; however, it works out fine when tested across all current versions of the major browsers.

  • Some online tools such as JSLint don't allow it
  • Multi-line strings can be dangerous in JavaScript because all hell breaks loose if you accidentally put a whitespace in between the escape character (\) and a new line. (@ripper234 comment)

Multiline String literals are disallowed by the Google Style Guide.

twotwotwo
  • 28,310
  • 8
  • 69
  • 56
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
  • With which browser standards? – Šime Vidas Dec 10 '12 at 20:07
  • That resource is out of date. Multi-line strings are specified in ECMAScript 5th edition (which was released in 2009). (It's in [section 7.8.4](http://ecma-international.org/ecma-262/5.1/#sec-7.8.4), in form of the *LineContinuation* production.) – Šime Vidas Dec 10 '12 at 20:39
  • 4
    When did IE start supporting it? Do IE 7/8 support it? – ripper234 Dec 11 '12 at 09:19
  • 2
    According to browsershot, even IE 6 does. So why does jshint warn about it by default? Just for style? http://browsershots.org/http://jsbin.com/ogefag/1# http://jsbin.com/ogefag/1 – ripper234 Dec 11 '12 at 10:38
  • 2
    `Multi-line strings can be dangerous in JavaScript because all hell breaks loose if you accidentally put a whitespace in between the escape character (\) and a new line. ` http://www.jshint.com/docs/ – ripper234 Dec 11 '12 at 10:41
  • ripper234 makes a good point, although any uglify task worth its salt would remove those whitespaces anyway. – jrz May 23 '13 at 20:39
  • @Ripper That rather offhand remark now appears in this documentation: http://jshint.com/docs/options/#multistr – Protector one Mar 25 '16 at 10:58
2

The accent grave (back-quote, back tick) character works like a quotation mark to define multiline strings in Javascript in Firefox and Google chrome, but not in Internet Explorer 11. These strings are called Template Literals and are part of the ES6 specification. I'm guessing that the generated newline sequence is what your editor generates, not what is expected on the computer that is interpreting the Javascript code.

Example:

var str=`This string
has three
lines.`;
Protector one
  • 6,926
  • 5
  • 62
  • 86
David Spector
  • 1,520
  • 15
  • 21