timknip avatar

javascript: interpolate variables in string

timknip

Published: 13 Sept 2018 › Updated: 13 Sept 2018javascript: interpolate variables in string

javascript: interpolate variables in string

Browser support:
Starting from Firefox 34 / Chrome 41 / Safari 9 / Microsoft Edge you can use an ES2015 / ES6 feature called Template Literals and use this syntax:

String text ${expression}

Template literals are enclosed by the back-tick (` `) (grave accent) instead of double or single quotes.

Example:

var a = 5;
var b = 10;
console.log(Fifteen is ${a + b}.);
// "Fifteen is 15.

How neat is that?

Bonus:

It also allows for multi-line strings in javascript without escaping, which is great for templates:

return <div class="${foo}"> ... </div>;

With Node.js v4 , you can use ES6's Template strings.

Leave javascript: interpolate variables in string to:

Written by

^_^

Read more #note-timknip posts


Best Posts From timknip

We have not curated any of timknip's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From timknip