George Bailey George Bailey - 1 year ago 206
Node.js Question

How do I do a multi-line string in node.js?

With the rise of node.js, multi-line strings are becoming more necessary in JavaScript.

  1. Is there a special way to do this in Node.JS, even if it does not work in browsers?

  2. Are there any plans or at least a feature request to do this that I can support?

I already know that you can use
at the end of every line, that is not what I want.

Answer Source

Javascript does not support multi-line strings but there are hacks.

See Creating multiline strings in JavaScript


As noted below, multi-line strings are now intrinsically supported in JavaScript, as so in NodeJS.

Several months after the original above was written, the new ES5 JavaScript parser was released that recognized a "line continuation" character allowing you to write multi-line strings such as:

'this is a \
single string'

which evaluates to: 'this is a single string'.

Note that the newline at the end of the first line is not included in the resulting string.

As of ES6 (and so versions of Node greater than v4), a new "template literal" intrinsic type was added to Javascript (denoted by back-ticks "`") which can also be used to construct multi-line strings, as in:

`this is a 
single string`

which evaluates to: 'this is a\nsingle string'.

Note that the newline at the end of the first line is included in the resulting string.

Template literals we added to allow programmers to construct strings where values or code could be directly injected into a string literal without having to use util.format or other templaters, as in:

let num=10;

console.log(`the result of ${num} plus ${num} is ${num + num}.`);

which will print "the result of 10 plus 10 is 20." to the console.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download