Meta tags:
Headings (most frequently used words):
the, javascript, for, web, functions, recursion, example, objects, recursive, simple, countdown, complex, tree, structure, summary, chapter, article, toc, about, getting, started, basics, control, structures, data, types, built, in, operators, classes, error, handling, regular, expressions, general, bom, dom,
Text of the page (most frequently used words):
the (68), #function (32), and (25), recursive (21), number (19), for (18), items (18), javascript (17), this (17), you (17), with (17), name (17), functions (16), example (14), countdown (13), that (12), tree (12), data (11), have (11), simple (11), level (11), let (11), introduction (11), which (9), but (9), just (9), files (9), item (9), are (8), call (8), indentation (8), can (7), need (7), our (7), will (7), printtree (7), not (6), itself (6), all (6), very (6), exe (6), how (6), like (6), operators (6), tutorial (5), has (5), values (5), they (5), folders (5), some (5), start (5), look (5), also (5), recursion (5), operator (5), article (4), when (4), calls (4), common (4), used (4), hierarchical (4), quite (4), would (4), windows (4), objects (4), structure (4), complex (4), methods (4), parameters (4), been (3), into (3), your (3), here (3), multiple (3), return (3), callback (3), from (3), really (3), programming (3), technique (3), many (3), null (3), generate (3), each (3), use (3), array (3), its (3), child (3), computers (3), more (3), there (3), show (3), user (3), web (3), object (3), regular (3), expressions (3), catching (3), fully (2), languages (2), language (2), see (2), specific (2), summary (2), set (2), once (2), because (2), sure (2), pass (2), takes (2), care (2), rest (2), bit (2), come (2), don (2), through (2), browser (2), work (2), filefoldertree (2), program (2), readme (2), txt (2), system32 (2), drivers (2), explorer (2), notepad (2), secret (2), stuff (2), repeat (2), console (2), log (2), complete (2), where (2), arrays (2), notice (2), increment (2), variable (2), recursively (2), string (2), take (2), print (2), any (2), could (2), found (2), listed (2), display (2), only (2), easily (2), etc (2), what (2), structures (2), world (2), than (2), likely (2), using (2), want (2), something (2), then (2), alert (2), special (2), asp (2), net (2), dom (2), window (2), bom (2), search (2), error (2), types (2), conditional (2), errors (2), try (2), catch (2), fields (2), inheritance (2), getters (2), setters (2), prototypes (2), anonymous (2), default (2), basics (2), loops (2), statement (2), toc (2), localization, suggest, correction, contact, com, 2007, 2026, translated, following, preferred, list, help, translate, click, czech, next, previous, referred, examples, useful, time, extremely, practical, solve, problems, dealing, after, called, already, discussed, last, line, motion, times, needed, simply, first, code, contain, normally, directly, underlying, system, since, access, recreated, place, combine, form, shown, few, lines, big, even, promised, prints, proper, amount, base, current, starting, bunch, hyphens, names, repeatedly, until, other, words, content, their, below, added, signal, parent, folder, belong, representing, computer, them, visual, representation, text, spice, visually, adding, icons, lot, illustrate, real, family, worked, couple, hours, seen, displayed, heavy, mix, techniques, haven, covered, yet, seems, advanced, stage, learning, process, feel, free, skip, back, later, quick, warning, recognize, above, was, perhaps, boring, illustrative, powerful, remedy, showing, illustrates, often, create, now, handles, everything, including, decreasing, iteration, allowing, taken, stop, check, bigger, making, properly, exited, soon, done, version, solution, works, definitely, elegant, fix, turning, mentioned, careful, stopping, point, otherwise, end, endless, loop, crash, environment, say, decrease, value, new, without, might, why, ever, actually, cases, especially, solving, tasks, essentially, unique, most, notation, meaning, add, keyword, declaration, anything, considered, wpf, php5, jquery, html5, css3, webforms, mvc, location, history, script, tag, general, replace, capturing, groups, match, regex, flags, options, throwing, finally, handling, static, classes, extension, property, accessors, properties, optional, chaining, nullish, coalescing, ternary, remainder, logical, modify, assign, decrement, arithmetic, comparison, assignment, json, math, date, built, type, conversion, undefined, strings, numbers, booleans, while, switch, control, truthy, falsy, variables, basic, syntax, hello, tools, getting, started, libraries, webbrowser, about, chapter, table, contents,
Text of the page (random words):
recursive functions the complete javascript tutorial javascript tutorial toc table of contents chapter article toc introduction function parameters function parameters with default values anonymous functions callback functions recursive functions a simple recursion example the countdown a complex recursion example the tree structure summary multiple return values about javascript what is javascript javascript and the webbrowser javascript libraries getting started the tools you need hello world the basics basic javascript syntax variables truthy falsy control structures the if statement the switch statement loops for and while loops for of and for in data types introduction booleans numbers strings null undefined type conversion built in objects introduction number string array the basics array methods date math json operators introduction the assignment operator comparison operators arithmetic operators increment decrement operators modify and assign operators logical operators remainder operator conditional ternary operator nullish coalescing operator optional chaining operator functions introduction function parameters function parameters with default values anonymous functions callback functions recursive functions multiple return values objects introduction properties methods property accessors getters setters prototypes prototypes inheritance extension classes introduction fields methods getters setters inheritance static fields methods error handling introduction catching errors try catch catching throwing errors try catch finally error types conditional catching regular expressions introduction regex flags options search match with regular expressions using capturing groups search replace with regular expressions javascript for web general introduction window dom bom the script tag javascript for web the bom the window object the history object the location object javascript for web the dom introduction javascript asp net mvc asp net webforms c css3 html5 jquery php5 wpf functions recursive functions a recursive function is essentially just a function that calls itself recursive functions are not unique to javascript its a very common programming technique which can be used with most programming languages with functions also there s no special notation for recursive functions in javascript meaning that you don t have to add a special keyword to the function declaration or anything like that if the function calls itself it is considered a recursive function but why would you ever need a function to call itself actually there are many use cases for this technique especially when you start solving more complex tasks but for this tutorial let s start with a simple example to show you how they work a simple recursion example the countdown let s say that you would like to do a simple countdown where we start with a number show it to the user decrease the value by 1 and then show the new number to the user we want a function to display the number to the user without the use of recursive functions it might look like this function countdown number alert number let number 3 countdown number countdown number countdown number this solution works but it s definitely not very elegant we can easily fix this by turning it into a recursive function but how as mentioned a recursive function just have to call itself to be recursive but you need to be careful how you do it there has to be something stopping the recursive calls at some point because otherwise you will end up with an endless loop which will likely crash the javascript environment e g the browser let s have a look at the recursive version of our countdown example function countdown number alert number number if number 0 countdown number let number 3 countdown number in our example the stop check is quite simple we only call the countdown function recursively if our number is bigger than 0 making sure that the function is properly exited as soon as the countdown is done also notice that now our function handles everything including decreasing the number on each iteration allowing us to just call the function once and then the rest is taken care of a complex recursion example the tree structure i recognize that the above example was very simple perhaps a bit boring and not quite illustrative of how powerful a recursive function can be i want to remedy that by showing you a more complex example which also illustrates something that recursion is often used for to create a tree structure quick warning in this example i will be using a heavy mix of arrays and objects and some techniques which haven t been fully covered in this tutorial yet if this seems to advanced for you at this stage of your learning process feel free to skip this example and come back to it later tree structures are used quite a lot on computers to illustrate hierarchical data but also in the real world like a family tree etc if you have worked with computers for more than a couple of hours you have likely seen its folders and files displayed as a tree structure for my example i would like to take some hierarchical data representing some of the folders and files found on a windows computer and display them as a tree in my example the visual representation is just a very simple text only tree but you could easily spice it up visually by adding icons etc here s what it will look like c program files common files start exe readme txt windows system32 drivers explorer exe notepad exe t data secret stuff really simple tree really simple content as it can be found on many windows computers folders and files are listed by their name and if they have any child folders and or files they are listed below with added indentation to signal which parent folder they belong to to generate this we need a function which will take an array of items print the names and for each item also print its child items if it has any it has to do this repeatedly until it has been through all items and all child items in other words we need a recursive function here s how it could look function printtree items level for let item of items let indentation repeat level 4 console log indentation item name if item items null printtree item items level 1 notice how few lines we need to generate that big tree the function is even very simple as promised it just prints the name with a proper amount of indentation we base the indentation on the current level starting with 0 and for each level of the tree we increment the level variable and pass it when we recursively call the printtree function we use the level variable to generate the indentation string which is just a bunch of hyphens so with that in place let s have a look at the complete example where we combine tree data in the form of objects in arrays with the function just shown let filefoldertree name c items name program files items name common files items name start exe name readme txt name windows items name system32 items name drivers name explorer exe name notepad exe name t items name data name secret stuff function printtree items level for let item of items let indentation repeat level 4 console log indentation item name if item items null printtree item items level 1 printtree filefoldertree 0 as you can see it takes quite a bit of code to contain all the folders and files as a hierarchical set of data normally this would just come directly from the underlying system but since we don t have access to that from javascript through the browser i have recreated some data for us to work with after all the data we have the recursive function called printtree as we already discussed on the very last line we have our simple call to the recursive function which will set it all in motion we just call it once because it will be sure to call itself as many times as needed i simply pass in our data and the first level 0 and our recursive function takes care of the rest summary when a function calls itself it s referred to as a recursive function and as you can see from the examples of this article this can be really useful recursive functions which are a common programming technique and not specific to javascript are not used all the time but they are extremely practical when you need to solve specific problems e g dealing with hierarchical data callback functions previous multiple return values next this article has been fully translated into the following languages czech is your preferred language not on the list click here to help us translate this article into your language javascript tutorial com 2007 2026 contact us suggest correction localization
|