เพื่อคืนค่าจากฟังก์ชัน JavaScript ให้ใช้คำสั่ง return ใน JavaScript คุณต้องรันโค้ดด้านล่างเพื่อเรียนรู้วิธีคืนค่า
ตัวอย่าง
<html>
<head>
<script>
function concatenate(name, age) {
var val;
val = name + age;
return val;
}
function DisplayFunction() {
var result;
result = concatenate('John ', 20);
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>