concat 2 strings in js code example

Example 1: string concatenation in js

var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);
console.log(res);

Example 2: how to concatenate strings javascript

var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);
// does not change the existing strings, but
// returns a new string containing the text
// of the joined strings.

Example 3: how to concatenate strings and variables in javascript

const helloName = name => `Hello ${name}!`

Example 4: js concatenate strings

const str1 = 'Hello';
const str2 = 'World';

console.log(str1 + str2);
>> HelloWorld

console.log(str1 + ' ' + str2);
>> Hello World

Example 5: concat no and string in javascript

['Hello', ' ', 'World'].join(''); // 'Hello World'

Example 6: js concat variable and string

const txt = "My name is"
console.log(`${txt} Paul`);