JavaScript 1.2 ช่วยให้สามารถซ้อนคำจำกัดความของฟังก์ชันภายในฟังก์ชันอื่นๆ ได้เช่นกัน ยังคงมีข้อจำกัดที่คำจำกัดความของฟังก์ชันอาจไม่ปรากฏในลูปหรือเงื่อนไข ข้อจำกัดเหล่านี้ในการกำหนดฟังก์ชันใช้เฉพาะกับการประกาศฟังก์ชันด้วยคำสั่งฟังก์ชันเท่านั้น
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อเรียนรู้วิธีใช้ JavaScript Nested Functions -
<html>
<head>
<script>
function hypotenuse(a, b) {
function square(x) { return x*x; }
return Math.sqrt(square(a) + square(b));
}
function secondFunction(){
var result;
result = hypotenuse(3,4);
document.write ( result );
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "secondFunction()" value = "Find Hypotenuse">
</form>
</body>
</html>