← #25 Copy a long variable from the Console

Use template literal to concatenate strings

Topics: JavaScript

There are some ways to concatenate variables in a string. It's recommended to use template literal syntax to avoid unexpected result when the types of variables vary.

const foo = 4;
const bar = 2;
const fuzz = 'Fuzz';

// Bad: Use + operator
foo + bar + fuzz; // '6Fuzz'

// Better: Use concat
''.concat(foo, bar, fuzz); // 42Fuzz
[foo, bar, fuzz].join(''); // 42Fuzz

// Best: Use template literal
`${foo}${bar}${fuzz}`; // 42Fuzz


Fix a typo or suggest an improvement
#27 Use negative nth-child and nth-last-child