For Example:
let originalStr = ['yesterday', 'I', 'go', 'to', 'school'];
I wanna get a full string as
'yesterday I go to school';
CodePudding user response:
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
CodePudding user response:
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(' ').
CodePudding user response:
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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
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'
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>