OneBite.Dev - Coding blog in a bite size

concatenate a string in Javascript

Code snippet on how to concatenate a string in Javascript

let str1 = 'Hello';
let str2 = 'World';
let strConcat = str1 + ' ' + str2;

console.log(strConcat); // Hello World

This code is concatenating two strings in Javascript. The first line of code declares the variable str1 and sets it equal to the string “Hello”. The second line of the code does the same for the variable str2 but giving it the string “World”. The third line of code uses the + operator to combine the two strings. The first argument is the variable str1 with a space afterwards. After that, str2 is added to the end. The resulting concatenated string is then stored in the variable strConcat. Finally, the concatenated string is printed to the console by calling console.log() and passing strConcat as an argument. The output will be the concatenated string “Hello World”.

javascript