JavaScript Multi-line Strings


Traditionally I’ve always used something like the following when writing multi-line strings …

var build = "";
build += "<div>";
build += " <label for='name'>name</label>";
build += " <input type='text' id='name' />";
build += "</div>";

In ES6 you can write multi-line strings much cleaner using backticks rather than single or double quotes …

var build = `
<div>;
<label for='name'>name</label>;
<input type='text' id='name' />;
</div>`;

… which is much cleaner!

A backtick is a diagonal single quote which is at the top left of my keyboard.

The latest versions of Chrome, Firefox, Edge and Safari all support this. Unfortunately IE doesn’t.