(Const VS Var) keywords

What is the point of using const in the tutorial instead of var keyword?

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}
console.log(countup(5));

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

In general, var should be avoided. const and let (in that order) are preferred.

1 Like

If I use var in the code above, will the result be the same?

In this specific case, yes. In general, often not.

You just shouldn’t use var.

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 .

That is a technique known as recursion:

I am at the switch statements so this is a little complicated for now, but thanx for that i’ll save it for later.

1 Like

It will be explained when you get there.

1 Like

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 .

Again, this will be covered later.

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.

1 Like

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.

1 Like

I can imagine. Thank you.

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.

function recursiveExample(count = 5) {
  if (count === 0) {
    return 0;
  } else {
    return recursiveExample(count - 1);
  }
}

So call it in the code:

recursiveExample()

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
3 Likes

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.

2 Likes

you can make self call function

syntax is

(function(){
// your code goes here
}) () ;

example :
(function(){
console.log(‘hello world’);
}) ();

// result : hello world in console

That isn’t the same thing as recursion though. That’s an immediately invoked function.

1 Like

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.

Two totally different cases.

I am well aware of the diffences between var, let, and const.

var should be avoided.

const should be used if possible.

let should be used if the value stored in the variable must be changed.

Your code should only use let or const. var is a legacy feature that can lead to unexpected behavior and bugs if you aren’t careful.