ฟังก์ชัน JavaScript สามารถมีตัวเลือก return คำแถลง. นี่เป็นสิ่งจำเป็นหากคุณต้องการคืนค่าจากฟังก์ชัน คำสั่งนี้ควรเป็นคำสั่งสุดท้ายในฟังก์ชัน
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อทำงานกับคำสั่ง return ใน JavaScript -
<html>
<head>
<script>
function concatenate(first, last) {
var full;
full = first + last;
return full;
}
function DisplayFunction() {
var result;
result = concatenate('John', ' Wright');
document.write (result );
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "DisplayFunction()" value = "Call Function">
</form>
</body>
</html>