0
How does nested functions work in javascript? Any examples?
Nested functions in javascript
1 Antwort
+ 2
Nested functions are simply functions in a function. You declare them inside a function block.
function total(array) {
function isNumber(o) {
return typeof o === "number";
}
var sum = 0;
for (var i = 0; i < array.length; i++) {
if (isNumber(array[i]) {
sum += array[i];
}
}
return sum;
}
total([5, 7, "6", 2]); // returns 14
Here `isNumber` is nested inside the function `total`.