append several elements at once to string in javascript

One of the ways to append several elements at once to a string in Javascript is to use the Array method join() in conjunction with the String method concat(). Here's an example:

index.tsx
const arr = ['hello', 'world', '!'];
const str = 'Hey, ';

const newStr = str.concat(arr.join(' '));
console.log(newStr); // Output: Hey, hello world !
152 chars
6 lines

In this example, we start with an array arr containing three string elements. We also have a string str that we want to append the elements of arr to. To do this, we call the join() method on arr with a space as an argument to create a new string with all the elements of arr separated by spaces. We then call the concat() method on str with the result from join() as an argument, which appends the new string to the end of str.

The output should be: Hey, hello world !.

gistlibby LogSnag