Last Updated: October 07, 2020
·
733
· jonabrams

How I write multi-line strings in JavaScript

Forget having to do the usual:

var bigString = 'Line 1\n' +
                'Line 2\n' +
                ...;

Instead, make an array and join with the newline character:

var bigString = [
  'Line 1',
  'Line 2',
  ...
].join('\n');

I find this makes writing, reading, and editing more than a couple lines a lot easier.

1 Response
Add your response

var props = [
  'I like it.',
  'Nice tip Jon!'
].join('\n');
over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#javascript
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside