ในการส่งคืนสตริงจากฟังก์ชัน JavaScript ให้ใช้ return คำสั่งใน JavaScript
ตัวอย่าง
คุณต้องเรียกใช้โค้ดต่อไปนี้เพื่อเรียนรู้วิธีส่งคืนสตริงโดยใช้คำสั่ง return -
<html>
<head>
<script>
function concatenate(name, subject) {
var val;
val = name + subject;
return val;
}
function DisplayFunction() {
var result;
result = concatenate('Amit', ' Java');
document.write (result );
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "DisplayFunction()" value = "Result">
</form>
</body>
</html>