要添加两个字符串,我们需要一个'+'运算符来在字符串之间创建一些空间,但是当第一个字符串本身包含空格时,就无需显式分配空间。
在下面的示例中,由于字符串'str1'中包含空格,因此仅串联 而没有空格就足以添加两个字符串。
<html>
<body>
<script>
function str(str1, str2) {
return (str1 + str2);
}
document.write(str("tutorix is the best ","e-learning platform"));
</script>
</body>
</html>输出结果
tutorix is the best e-learning platform
如果第一个字符串中没有空格,则必须创建space(“”)并将两个字符串连接起来,如下所示。
<html>
<body>
<script>
function str(str1, str2) {
return (str1 + " " + str2);
}
document.write(str("tutorix is the best","e-learning platform"));
</script>
</body>
</html>输出结果
tutorix is the best e-learning platform