javascript: interpolate variables in string

in #note-timknip6 years ago

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.

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 64359.90
ETH 3105.50
USDT 1.00
SBD 3.87