-2

For Example:

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

I wanna get a full string as

'yesterday I go to school';
4
  • 2
    Use array#join(). Commented Oct 21, 2021 at 19:16
  • 3
    originalStr.join(' ') Commented Oct 21, 2021 at 19:17
  • 1
    Does this answer your question? Implode an array with JavaScript? Commented Oct 21, 2021 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?) Commented Oct 21, 2021 at 19:55

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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(' ').

Comments

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'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.