prepend a string to the beginning of another string in javascript

To prepend a string to the beginning of another string in JavaScript, we can use string concatenation or template literals. Here are the examples:

Using string concatenation:

index.tsx
let str1 = 'world';
let str2 = 'hello ';
let newStr = str2 + str1;
console.log(newStr);  // output: 'hello world'
114 chars
5 lines

Using template literals:

index.tsx
let str1 = 'world';
let str2 = 'hello ';
let newStr = `${str2}${str1}`;
console.log(newStr);  // output: 'hello world'
119 chars
5 lines

In the above examples, we have concatenated the string "hello " and "world" to create a new string "hello world".

gistlibby LogSnag