o9s
is a microblog
home archives search feed blogroll


Template Strings

Google says that string concatenation should be done with template strings. To use template strings, use backticks instead of single (or double) quotes and wrap what you want to concatenate in a ${} construct.

let favoriteAnimal = 'dog';
console.log(`My favorite animal is the #{favoriteAnimal}`);

You can also do maths in template strings (but you probably shouldn’t).

let x = 5;
console.log(`x squared is ${x * x}`);

What I used to do for string concatenation, and still do in many languages is something like this.

let favoriteAnimal = 'dog';
console.log('My favorite animal is the ' + favoriteAnimal);
Posted on 2018-04-05   #javascript  






← Next post    ·    Previous post →