JavaScript – Create Multiline Strings in ES5/ES6
In this post, we show you the ways how to create multiline strings in JavaScript ECMAScript 5 and ECMAScript 6.
1. ECMAScript 5 (ES5)
- You can either use string concatenation (it is recommended according to Google’s JavaScript style guide, you can click here to get more details):
var message = 'This is just a sample text' +
'that demonstrates how to create' +
'multiline strings in JavaScript ES5...';
- or escaping newlines:
var message = 'This is just a sample text\
that demonstrates how to create\
multiline strings in JavaScript ES5...';
2. ECMAScript 6 (ES6)
ES6 introduces template strings to improve the capabilities of JavaScript strings. It has many features and one of them is back-ticks (``
). Here is a simple code that demonstrates how to use back-stick in ES6 template strings.
const message = `This is just a sample text
that demonstrates how to create
multiline strings in JavaScript ES6...`;