All Articles

How to build/concatenate a string in JavaScript

You can use Template Literals (Strings) in ES6 to concatenate strings. Also, you can use string interpolation and multi-line strings here.

You need to use the backtick (` `) character instead of double or single quotes to get the literals power.

let name = 'Bob';

// Logs "My name is Bob"
console.log(`My name is ${name}`);

let a = 15;
let b = 10;

// Logs "I'm 25"
console.log(`I'm ${a + b}`);

// Logs "I love MTB", because of all love MTB...
console.log(`I ${a > b ? 'love' : 'enjoy'} MTB`)

// Logs 
// "string  line 0
// string  line 1"
console.log(`string  line 0
string  line 1`);