Using Named Function Expressions:

You can give a function expression a name that is actually private and is only visible from inside of the function ifself:

var factorial = function myself (n) {
    if (n <= 1) {
        return 1;
    }
    return n * myself(n-1);
}
typeof myself === 'undefined'

Here myself is visible only inside of the function itself.

You can use this private name to call the function recursively.

See 13. Function Definition of the ECMAScript 5 spec:

The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.

Please note that Internet Explorer up to version 8 doesn't behave correctly as the name is actually visible in the enclosing variable environment, and it references a duplicate of the actual function (see patrick dw's comment below).

Using arguments.callee:

Alternatively you could use arguments.callee to refer to the current function:

var factorial = function (n) {
    if (n <= 1) {
        return 1;
    }
    return n * arguments.callee(n-1);
}

The 5th edition of ECMAScript forbids use of arguments.callee() in strict mode, however:

(From MDN): In normal code arguments.callee refers to the enclosing function. This use case is weak: simply name the enclosing function! Moreover, arguments.callee substantially hinders optimizations like inlining functions, because it must be made possible to provide a reference to the un-inlined function if arguments.callee is accessed. arguments.callee for strict mode functions is a non-deletable property which throws when set or retrieved.

Answer from Arnaud Le Blanc on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Recursion
Recursion - Glossary - MDN Web Docs
A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (resumes recursion). The following Python code defines a function that takes a number, prints it, and then calls itself again with the number's value -1. It keeps going until the number is equal to ...
🌐
GitHub
github.com › mdn › advanced-js-fundamentals-ck › blob › main › tutorials › 02-functions › 04-recursion.md
advanced-js-fundamentals-ck/tutorials/02-functions/04-recursion.md at main · mdn/advanced-js-fundamentals-ck
function factorial(n) { function recur(n, acc) { if (n <= 1) { return acc; } else { return recur(n-1, n*acc); } } return recur(n, 1); } factorial(40000); //Still an error now, will work later · Until JavaScript engines implement tail call optimization, if you have a recursive function which may make lots of nested calls you'll need to use a technique called trampolining.
Author   mdn
🌐
Programiz
programiz.com › javascript › recursion
JavaScript Recursion (with Examples)
The counter() function first displays count then checks if the value of count is greater than 1 with count > 1. If count > 1 evaluates to true, the program decreases the value of count and calls counter() with the new value of count (recursion).
Top answer
1 of 6
152

Using Named Function Expressions:

You can give a function expression a name that is actually private and is only visible from inside of the function ifself:

var factorial = function myself (n) {
    if (n <= 1) {
        return 1;
    }
    return n * myself(n-1);
}
typeof myself === 'undefined'

Here myself is visible only inside of the function itself.

You can use this private name to call the function recursively.

See 13. Function Definition of the ECMAScript 5 spec:

The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.

Please note that Internet Explorer up to version 8 doesn't behave correctly as the name is actually visible in the enclosing variable environment, and it references a duplicate of the actual function (see patrick dw's comment below).

Using arguments.callee:

Alternatively you could use arguments.callee to refer to the current function:

var factorial = function (n) {
    if (n <= 1) {
        return 1;
    }
    return n * arguments.callee(n-1);
}

The 5th edition of ECMAScript forbids use of arguments.callee() in strict mode, however:

(From MDN): In normal code arguments.callee refers to the enclosing function. This use case is weak: simply name the enclosing function! Moreover, arguments.callee substantially hinders optimizations like inlining functions, because it must be made possible to provide a reference to the un-inlined function if arguments.callee is accessed. arguments.callee for strict mode functions is a non-deletable property which throws when set or retrieved.

2 of 6
10

You can access the function itself using arguments.callee [MDN]:

if (counter>0) {
    arguments.callee(counter-1);
}

This will break in strict mode, however.

🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Functions
Functions - JavaScript - MDN Web Docs
A function that calls itself is called a recursive function. In some ways, recursion is analogous to a loop.
🌐
Selftaughttxg
selftaughttxg.com › 2023 › 01-23 › how-to-write-a-recursive-function-in-javascript-for-beginners
How to Write a Recursive Function in JavaScript for Beginners |
January 2, 2023 - Understanding how to set the initial value in the reduce method is essential for Dan's recursive function; he sets the initial value to an empty array. MDN web docs explains that an arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate limitations in usage:
🌐
CodinGame
codingame.com › playgrounds › 9891 › recursion-in-javascript-non-informative-content
Recursion in javascript (non-informative content)
CodinGame is a challenge-based training platform for programmers where you can play with the hottest programming topics. Solve games, code AI bots, learn from your peers, have fun.
Find elsewhere
🌐
Medium
medium.com › @vickdayaram › recursion-caad288bf621
Recursion. When I first was introduced to the… | by Pradeep Dayaram | Medium
July 5, 2017 - Recursion: An act of a function calling itself. Recursion is used to solve problems that contain smaller sub-problems. A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (continues recursion). MDN
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Errors › Too_much_recursion
InternalError: too much recursion - JavaScript - MDN Web Docs
July 8, 2025 - A function that calls itself is called a recursive function. Once a condition is met, the function stops calling itself.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript recursive function
JavaScript Recursive Function
November 15, 2024 - The function returns the sum of n and the result of calling itself with the argument (n - 1). This is where the recursion happens.
🌐
SitePoint
sitepoint.com › blog › javascript › recursion in functional javascript
Recursion in Functional JavaScript — SitePoint
November 11, 2024 - M. David Green demonstrates the powerful, but dizzying concept of recursion by refactoring normal for and while loops to use functions that call themselves.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Functions.
Functions - JavaScript | MDN
A function that calls itself is called a recursive function. In some ways, recursion is analogous to a loop.
🌐
DEV Community
dev.to › kadeesterline › recursion-in-javascript-3jk4
Recursion in JavaScript - DEV Community
June 28, 2022 - When returning num * recursiveFactorial(num -1) the original function waits for this new call to resolve. This continues until the base case is met. So, calling recursiveFactorial(3) is the same as calling 3 * 2 * 1. For more information on recursion and the call stack you can checkout these links: MDN · javascript.info ·
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-understand-recursion-in-javascript
Recursion in JavaScript - GeeksforGeeks
The recursive case is the part of the function where it calls itself with a modified input to move closer to the base case.
Published   January 16, 2026
🌐
freeCodeCamp
freecodecamp.org › news › what-is-recursion-in-javascript
What is Recursion? A Recursive Function Explained with JavaScript Code Examples
February 4, 2021 - The same goes for writing a recursive function. Always create the base case first and then write an argument that runs the recursive call at least once. The rest will be easier from there. If you enjoyed this article and want to take your JavaScript skills to the next level, I recommend you check out my new book Beginning Modern JavaScript here.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Recursion and stack
Any recursion can be rewritten as a loop. The loop variant usually can be made more effective. …But sometimes the rewrite is non-trivial, especially when a function uses different recursive subcalls depending on conditions and merges their ...