หากต้องการเพิ่มสองสตริง เราจำเป็นต้องมีตัวดำเนินการ '+' เพื่อสร้างช่องว่างระหว่างสตริง แต่เมื่อสตริงแรกมีช่องว่างอยู่ในนั้น ไม่จำเป็นต้องกำหนดพื้นที่อย่างชัดเจน
ในตัวอย่างต่อไปนี้ เนื่องจากสตริง '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
ในกรณีที่ไม่มีช่องว่างในสตริงแรก เราต้องสร้างช่องว่าง (" ") และรวมสองสตริงดังที่แสดงด้านล่าง
ตัวอย่าง
<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