-2

For Example:

let originalStr = ['yesterday', 'I', 'go', 'to', 'school'];

I wanna get a full string as

'yesterday I go to school';
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
John Kent
  • 1
  • 2
  • 2
    Use [`array#join()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join). – Hassan Imam Oct 21 '21 at 19:16
  • 3
    `originalStr.join(' ')` – CherryDT Oct 21 '21 at 19:17
  • 1
    Does this answer your question? [Implode an array with JavaScript?](https://stackoverflow.com/questions/4146927/implode-an-array-with-javascript) – Dylan Lee Oct 21 '21 at 19:47
  • John, welcome. Before asking for help, it would have been better to look around to see if your question has already been asked or at least try to google it. (read [What topics can I ask about here?](https://stackoverflow.com/help/on-topic)) – prietosanti Oct 21 '21 at 19:55

3 Answers3

0

I would just use the Array.prototype.join() method:

let strArray = ['yesterday', 'I', 'go', 'to', 'school']
console.log(strArray.join(' '))

output: 'yesterday I go to school'

The parameter passed to join is your separator string.

For more information about it you can check out the docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

Dosmond
  • 1
  • 2
0

you should use the join() function, it will concatenate the strings and insert the selected character inside the (), for example:

originalStr.join('-') will result in "yesterday-i-go-to-school", but you can absolutely use empty spaces like join(' ').

0

You can use concat() method for concatenation. for example:

var string1 = 'Hello';
var string2 = 'World';
var string3 = '!';
var result = '';
result = string1.concat(' ', string2); //result = 'Hello World'
result = result.concat('', string3);   //result = 'Hello World!'
console.log(result);

Also you can use join() if you have an array of strings and want to concatenate them together.

var array = ['yesterday', 'I', 'go', 'to', 'school'];
console.log(array.join(' ')); //array = 'yesterday I go to school'