admin管理员组

文章数量:1421542

Sorry if this question is to basic but how do I put a space in-between my two strings using JavaScript ?

The code in question is the following

console.log ("myFirstString + mySecondString =  " + (myFirstString +  mySecondString));

The out put is ing out as "myFirstString + mySecondString = MikeSlett"

I need there to be a space between Mike and Slett soo it reads " Mike Slett".

Thanks for any help!

Sorry if this question is to basic but how do I put a space in-between my two strings using JavaScript ?

The code in question is the following

console.log ("myFirstString + mySecondString =  " + (myFirstString +  mySecondString));

The out put is ing out as "myFirstString + mySecondString = MikeSlett"

I need there to be a space between Mike and Slett soo it reads " Mike Slett".

Thanks for any help!

Share Improve this question asked Jul 13, 2021 at 20:34 PoisonMasterPoisonMaster 471 silver badge6 bronze badges 2
  • Well, how did you put the space between = and MikeSlett? Any particular reason why you think string concatenation wont work on other places as well? – derpirscher Commented Jul 13, 2021 at 20:41
  • Just needed the "" instead of one " so now I understand :) – PoisonMaster Commented Jul 13, 2021 at 20:48
Add a ment  | 

3 Answers 3

Reset to default 3
myFirstString + " " + mySecondString

In modern JavaScript you can use a template literal. Then just put a space between the variable substitutions.

console.log(`myFirstString + mySecondString = ${myFirstString} ${mySecondString}`)

[stringA, stringB].join(' ');

runs a bit faster than,

stringA + ' ' + stringB;

本文标签: javascriptHow do i put a space between my two stringsStack Overflow