连接 JavaScript 句子时如何留出空格



我正在将特定的单词插入到一个带有串联的句子中,但单词一起运行,它们之间没有空格。

var adjective1 = + 'amazing';
var adjective2 = + 'fun';
var adjective3 = + 'entertaining';

var madLib = 'The Intro to JavaScript course is'   + adjective1 +  'james and Julia are so'  + 
adjective2 + 'I cannot wait to work through the rest of this' + adjective3 + 'content!';
console.log(madLib);

只需添加空格

var madLib = 'The Intro to JavaScript course is '   + adjective1 +  ' james and Julia are so'  + 
adjective2 + 'I cannot wait to work through the rest of this ' + adjective3 + ' content!';

您也不应该在变量赋值中的字符串之前有+。这将尝试将字符串转换为数字,并且会产生NaN因为它们不是数字。

var adjective1 = 'amazing';
var adjective2 = 'fun';
var adjective3 = 'entertaining';

最新更新