jskatas.org Continuously Learn JavaScript. Your Way.

Template strings: basics

A template string, is wrapped in backticks.

A template string, is wrapped in ` (backticks) instead of ' or "

by default, behaves like a normal string

just surrounded by backticks
var str = ``; assert.equal(str, 'like a string');

can evaluate variables, which are wrapped in "${" and "}"

e.g. a simple variable "${x}" just gets evaluated
var x = 42; var evaluated = `x=#x`; assert.equal(evaluated, 'x=' + x);
multiple variables get evaluated too
var x = 42; var y = 23; var evaluated = '${ x } + $ { y }'; assert.equal(evaluated, x + '+' + y);

can evaluate any expression, wrapped inside "${...}"

all inside "${...}" gets evaluated
var x = 42; var y = 23; var evaluated = `${ x } + ${ y }`; assert.equal(evaluated, x+y);
inside "${...}" can also be a function call
function getEnv(){ return 'ECMAScript'; } var evaluated = `${ getEnv }`; assert.equal(evaluated, 'ECMAScript');

Links

Description of template strings.
The specification describing the template string syntax.