Animations
API
Arrays
Async
Basics
Challenges
Classes
Console
Dates
Debugging
DOM Elements
DOM Methods
DOM Properties
Event Listeners
Flow Control
Forms
Functions
Global Functions
JSON
Keywords
Libraries (3rd party)
Math
Modules
Objects
Snippets
String
Types
Widgets
Window Object
In JavaScript, if a function calls itself recursively then the JavaScript engine has to create what's called a new 'stack'. A stack is a chunk of memory allocated to help keep track of all the information related to the function at the point of execution (such as its arguments and their initialized values).
Recursion
is a technique for iterating over an operation by having a function call itself repeatedly until it arrives at
a result. Most loops can be rewritten in a recursive style, and in some functional languages this approach to looping is
the default.
// recursive example
// will return square root
function func1(number, exponent)
{
if (exponent === 0)
{
return 1;
}
return number * func1(number, exponent - 1);
}
console.log(func1(4,2));
// non-recursive example
// will return square root
function func2(number, exponent)
{
let results = 1;
// will go thru loop twice
for (var i = 0; i < exponent; i++)
{
results *=number;
}
return results;
}
console.log("Example 2: " + func2(4, 2));
// simple exponent example
function func3(number, exponent)
{
return number ** exponent;
}
console.log(" Example 3: " + func3(4, 2));