Try

function findCategory(categoryName) {
    var trail = [];
    var found = false;

    function recurse(categoryAry) {

        for (var i = 0; i < categoryAry.length; i++) {
            trail.push(categoryAry[i].category);

            // Found the category!
            if ((categoryAry[i].category === categoryName)) {
                found = true;
                break;

                // Did not match...
            } else {
                // Are there children / sub-categories? YES
                if (categoryAry[i].children.length > 0) {
                    recurse(categoryAry[i].children);
                    if(found){
                        break;
                    }
                }
            }
            trail.pop();
        }
    }

    recurse(catalog);

    return trail
}

Demo: Fiddle

Answer from Arun P Johny on Stack Overflow
🌐
Medium
medium.com › @psutcl › breaking-down-basic-recursion-in-javascript-77663aec0771
Breaking Down Basic Recursion in JavaScript | by Polly Sutcliffe | Medium
April 12, 2020 - When you write a recursive function, it’s important to have a base case. The base case tells the recursive part what conditions should be met in order for the recursive function to stop calling itself. If you don’t include it, the recursive part will never stop, and your computer will eventually give you an error or hang, neither of which is very fun.
Discussions

Break out of Javascript recursive function on .click()
I want the slider to autoplay on ... using a recursive (I think?!!) function called autoPlay() (see line 50). However, I want the user to be able to interact with the slider using previous/next and pagination buttons. I have set these up and they work (see below autoPlay function). The problem is that autoplay and the navigation don't work together. I can't get the autoPlay function to stop / break when user ... More on teamtreehouse.com
🌐 teamtreehouse.com
2
November 5, 2014
how to break a recursive callback function in javascript - Stack Overflow
Here i passed a callback function which can call itself during runtime and prints a value incremented by one each time it gets called.I want it to break after a certain value.How i can do that? i am More on stackoverflow.com
🌐 stackoverflow.com
March 31, 2016
javascript - How to get out of Recursive function? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
February 25, 2013
javascript - Recursive function break - Stack Overflow
What I have trouble with is why the continueWalk flag returns back to true after it's been set to false by the callback. The intention is that it should break the loop at that point, and all loops from the recursive functions above. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Webdevtutor
webdevtutor.net › blog › javascript-break-recursive-function
How to Break a Recursive Function in JavaScript
Another method is to introduce a flag variable that can be toggled to stop the recursive calls when necessary. This approach gives you more flexibility in breaking out of the recursion at different points within the function.
🌐
Webdevtutor
webdevtutor.net › blog › javascript-break-out-of-recursive-function
How to Break Out of a Recursive Function in JavaScript
Another method to break out of a recursive function is by throwing an exception when the exit condition is met. This can be an effective way to immediately stop the recursion and handle the exceptional case outside of the recursive function. Here's how you can implement this approach: javascript ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-program-to-forced-exit-from-recursion
JavaScript Program to Forced Exit from Recursion | GeeksforGeeks
September 22, 2023 - However, during recursive operations, there may be scenarios where we need to forcibly exit the recursion to prevent an infinite loop or avoid unnecessary computations. In this article, we'll explore common situations where forced exit from recursion is necessary and how to implement solutions to handle them in JavaScript, specifically in the context of React components.
🌐
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 - This causes the function to call itself, again and again, making it infinitely recursive. This issue also appears if the same variable is used in the getter. ... To avoid this problem, make sure that the property being assigned to inside the setter function is different from the one that initially triggered the setter.
🌐
Programiz
programiz.com › javascript › recursion
JavaScript Recursion (with Examples)
Finally, the returned value from factorial(3) is stored in the result variable. ... Yes, JavaScript does have a recursion limit. The recursion limit prevents errors caused by too many nested function calls.
Find elsewhere
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Recursion and stack
So, the recursion reduces a function call to a simpler one, and then – to even more simpler, and so on, until the result becomes obvious. ... A recursive solution is usually shorter than an iterative one. Here we can rewrite the same using the conditional operator ? instead of if to make pow(x, n) more terse and still very readable:
🌐
DEV Community
dev.to › kriegercisneros › recursion-in-js-for-beginners-avoiding-and-infinite-loop-17bh
Recursion in JS for Beginners, avoiding an infinite loop - DEV Community
February 20, 2023 - JavaScript is throwing us this error because we are recursively calling recursionFunction outside of our base case declaration (the "if" statement right at the beginning). Think about it like this: we are calling a function from the body of itself. Which means we are inherently setting up the stage for an infinite loop. If we don't tell JS, "Hey wait! Stop running this loop when you hit zero. You don't need to go past zero and into the forever underworld of negative numbers.
🌐
HackerNoon
hackernoon.com › rethinking-javascript-break-is-the-goto-of-loops-51b27b1c85f8
Rethinking JavaScript: Replace break by going functional | HackerNoon
January 15, 2017 - In my last article, Death of the for Loop, I tried to convince you to abandon the for loop for a more functional solution. In return, you raised a great question, “What about break?”
🌐
HowDev
how.dev › answers › introduction-to-recursion-in-javascript
Introduction to recursion in JavaScript
October 7, 2020 - Sometimes, in the case of such small functions, using a loop is the better choice. In JavaScript, it is generally cheaper to run a loop than it is to call a function multiple times, and the time complexity will be higher with a recursive function. Still, there are multiple instances where loops are not worth it.
🌐
Stack Overflow
stackoverflow.com › questions › 23018916 › javascript-recursion-breaking-out
Javascript Recursion: Breaking out - Stack Overflow
Although I'm unsure exactly how to implement what he suggested. Xan, might you have any suggestions? ... I think what's mixing you up is you have async IO going on in there. Recursion is about calling the current function and maintaining a call stack, but because you're dealing with callbacks, that doesn't really happen like you think.
🌐
Medium
medium.com › functional-javascript › recursion-282a6abbf3c5
Recursion. Recursion, Tail Calls, Proper Tail… | by Santosh Rajan | Functional JavaScript | Medium
April 11, 2014 - Every recursive function must have a break condition. Otherwise calling the function will lead to an infinite loop. A tail call is a function call whose return value is used as the callers return value. In the function above return factorial(n — 1, n * accumulator) is a tail call. In JavaScript ...
🌐
Suliworld
suliworld.com › 2022 › 03 › 06 › recursive-functions-with-javascript
Recursive functions with Javascript – suliworld.com
Now console.log(fibonacci(50)) will only take seconds to display the result, and the same for console.log(fibonacci(200)) We have seen how recursion works with javascript, and we also introduced dynamic programming to increase performance.
🌐
Medium
medium.com › @swellpf › understanding-recursion-doesnt-have-to-keep-you-stuck-in-a-loop-here-s-how-to-break-out-99ad189e78cd
Understanding Recursion Doesn’t Have to Keep You Stuck in a Loop. Here’s How to Break Out. | by Paul Cinoman | Medium
August 25, 2021 - A properly constructed recursive function consists of two components. The first is called the base case. The base case identifies the condition that will cause the loop to exit. The second requirement is that the input the function receives is different as the loop repeats and at some point reaches the base case.