Search within TutorialsPark

Javascript Closures and Recursions


More advanced applications of Javascript functions.


Javascript Function: Closures

In Javascript, closures are functions that have access to variables from another functions scope.This is achieved by creating a function within a function

The outer function creates a reference to the inner anonymous function, and thus making it possible to call the inner function via the reference.

The idea of closures it that the local variables must remain accessible to inner functions even when it seems it is out of scope.

Syntax: Javascript Function Closures

Example: Javascript Function Return Values

Give it a TRY! » Note: All javascript functions are closures, as they are objects and have a scope chain associated with them.


Javascript Functions: Recursion

By definition, a recursive function is created when a function calls itself by name.

In Recusion, the program starts executing at the beginning of the function and backs up to where it was when it called the function and starts executing from that point, if the test condition is satisfied.

But most importantly, there must be a way to stop the recursion at some point, or else it would be infinite, ultimately causing the program to crash.

Syntax: Javascript Anonymous Function as a Variable

Example: Javascript Functions - Recursion

Give it a TRY! » Note: For each iteration the function fibo() is called.