24.
What is the term used to describe a function that is defined within the body of another function and cannot be accessed outside of that function?
Arrow function
Inner function
Anonymous function
Nested function
Correct: B, D
A nested function, also known as an inner function, is a function that is defined within the body of another function. It is not accessible outside of that function and can only be invoked from within the parent function. Nested functions are commonly used to create private variables and encapsulate code within a specific scope.
Example:
function outerFunction() {
// This is the outer function
const outerVar = 10;
function innerFunction() {
// This is the inner function
const innerVar = 5;
console.log(outerVar + innerVar);
}
return innerFunction; // Returning the inner function
}
const myFunction = outerFunction(); // myFunction now holds the inner function
myFunction(); // Calling the inner function