From the course: Learning Functional Programming with JavaScript (ES5)

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Recursion

Recursion - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript (ES5)

Start my 1-month free trial

Recursion

- [Narrator] The next advanced topic we're going to talk about is recursion. Now a recursive function is basically just a function that calls itself somewhere within its definition. While for reasons of efficiency, you generally want to avoid using recursive functions, they can make certain programming problems a lot easier. This includes problems such as tree traversal, which using a regular, procedural approach, can be quite clumsy sometimes. Let's go over some examples. Using recursion, it's possible to create structures that behave like a loop. The way we do this is simply by calling the function within itself with different arguments, and then defining conditions under which the functions should stop calling itself. In this case, we want our function to stop calling itself as soon as I reaches zero. Let's log the value of I so that we can see what happens when we call this function. Finally, lets call loop with a value of ten. Now let's run our code. As we can see, our loop…

Contents