How is it possible to use a function call inside the function itself ?
I am just wondering, i am not trying to answer you problem here. Sorry if i got my head in between you guys .
How can JavaScript not done reading the function and execute its own call ?
Does javascript leave it and come back to it when its done reading the whole thing?
I can’t imagine but it is possible i can see .
In terms of compiling, I assume that it compiles it all first, with a section for the code for the function, which is just referenced with a memory address. All this happens before the code is run. Don’t confuse compiling the code with executing the code. (If my understanding is wrong here, someone can correct me.) We don’t normally need to know about these things in JS, but if you want you can read about how JS is parsed, compiled, and run.
As mentioned, this is a common technique and has been around a long time. But yeah, it is a little trippy and can take a while to fully understand. You will encounter this later in the curriculum. It will most likely melt your brain a little - it was probably a year before I finally wrapped my head around what was happening in recursion - and even now my grasp is tenuous. Most of us don’t need recursion often, but when you do need it, it is amazingly powerful.
It appears to be needed just in math problems, do you think i will need recursion when building a social network like this forum for exemple?
Because if not i will be relieved.
It is not ‘just math problems’ where recursion is useful. Recursion comes in handy in navigating complex data structures. Also, learning recursion helps identify weaknesses in your understanding of key concepts, like return values, scope, function calls vs function definitions, etc.
It doesn’t execute its own call, it’s a new function every time. Each one of those functions is a copy of the original one (with whatever the value of the variables it uses stored alongside it). It’s already evaluated the function and stored it at the point it the function calls itself, so the code that processes JavaScript cam just make a copy of that stored function. Then it just executes the functions in the reverse order that they came in.
Basically, it puts each reference to each function in a stack (like a literal stack of items IRL). Then when it reaches the end, the last return, it can clear the stack, executing those functions from the last one added to the first one added.
Then as an example, note first function is at the bottom, once gets to the top, then it can be cleared off top-to-bottom:
return concrete value (0)
#6 is a func (copy of recursiveExample), count is 0
#5 is a func (copy of recursiveExample), count is 1
#4 is a func (copy of recursiveExample), count is 2
#3 is a func (copy of recursiveExample), count is 3
#2 is a func (copy of recursiveExample), count is 4
#1 is a func, recursiveExample, count is 5
Umm, so if you were just building some small part of a forum, like, say, some of the UI, sure, it’s very unlikely you’d be using certain programming idioms (like recursion). If you were personally building the core of something extremely complex, like a the logic of a social network (which is, admittedly, highly unlikely), then sure, a social network is normally modelled as a graph and recursion is commonly used to traverse graphs. The example you’ve picked happens to be one where recursive algorithms are a natural fit.
Replying to the OP - You can check out this video for a primer. Your original question has to do with scope. var leaks out into the global scope, whereas const and let stay in the block scope. This means you’ll have less conflicts across functions and parts of your program. Also, const has the benefit of not being (as) reassignable.
First heard of, seriously… VAR suggests that it will have variable value ( value will change factorwise ), whereas const sugests that value will stay the same no matter what.