function fib(n){
  let result =[0,1];
  for(let i=2;i<=n-1;i++){
const a =result[i-1];
const b = result[i-2];
result.push(a+b);
  }

return result;
}
console.log(fib(4));

// In the above code snippets everything is working fine only confusion is regarding the thesis behind the const keyword ,as according to ES6 we cannot change the value of variable with const keyword ,but in the above code snippets there is a continuous changing of the value for the variable a and b

Write your answer…

4 answers

You write const inside your for.. loop. Const is block-bound, so it only lives inside the body of your loop for that one iteration. The next iteration, it is gone completely and forever, and a new const variable with the same name can be created.

// foo is undefined here

(() => {
  const foo = 42;
  // const foo = 17; // error! foo already exists
})();

// foo is undefined here

const foo = 9999; // ok, because the previous `foo` only existed in the arrow function

according to ES6 we cannot change the value of variable with const keyword

That's not correct. You actually can. All you cannot do is change the variable's binding. So, after initially assigning a value to a const, you cannot do that a second time, but const does not care what happens inside the variable:

const a = 5;
a = 6; // error: cannot re-assign a value to `a`

const b = { foo: 42 };
b.foo = 17; // ok
Star1
Spot On1

Good explanation of one of the details that makes me wonder what the point of const even is... since it clearly does NOT behave like what other languages would consider a constant. In that example of the object properties changing, the behavior is more akin to a fixed pointer to an object, and not an object constant.

JavaScript really needs DEFINES.... REAL constants. Be nice if there was a mechanism for declaring them in the global scope from the local scope too since if we're going to have the over the top block-level, we should also have its counterpart. Otherwise you might as well dial the language back to "forward declaration only" Wirth style. Which would confuse the {expletive omitted} out of anyone coming from languages like C.

But this is what happens when "avoid using globals it's bad practice" gets magically transformed into paranoid "NEVUR DEW THATSIES!!!". Something that seems to be happening far, FAR too often in programming to the wrong things. (see HTML)

Reply to this…

Hashnode is building a friendly and inclusive dev community. Come jump on the bandwagon!

  • 💬 A beginner friendly place

  • 🧠 Stay in the loop and grow your knowledge

  • 🍕 >500K developers share programming wisdom here

  • ❤️ Support the growing dev community!

Register ( 500k+ developers strong 👊)

const (like let) is block restricted, so if you declare it inside {} it only exists for {}, and it isn't hoisted outside that block. So what's happening here is that EVERY time that code loops they are created, but when it hits that } for the "for" they are destroyed.

... and since they are destroyed / no longer exist, it's ok to recreate them again when the code loops.

Though say hello to memory thrashing and garbage collection hell. Part of why I wouldn't be creating those "variables for nothing" in the first place... and why I'm NOT a fan of const or let... or block level scoping.

Particularly when let can be outright silly -- see the one you have that really has no functional difference from var given where it is placed. That some people (as evidenced by linters) are now saying "NEVER USE VAR!!!" -- argh... Almost as bad as the "never use tables" folks.

Still say if you need something 'more' than function level scope you're probably doing something wrong -- but that could just be that I learned forward declarative high level languages and assembly first some four decades ago. The mere idea of a language letting you create variables on the fly in the middle of the code is still a really bad idea to my mind.

Cup1
Spot On1

awesome explanation..

Reply to this…

Have you reset the value of const anywhere else in the code? Nope... you haven't. So const is working fine. The code itself is not really changing the value of const, in fact, every time your function calls itself recursively, a new instance of the variables are created for the call. Try to understand what actually happens during recursion and you will be able to answer your question all by yourself.

Show all replies

The concept that goes for recursion works here as well. Take a look at the FOR loop, the scope of the variables are destroyed everytime the loop completes a circuit. I hope you get my point.

Reply to this…

When you define const you can initially assign value to it as constant value. But later on in code you can't change it. Meaning if you try to assign something later to it it won't allow you.

const a = 3;
a = 4 // this won't work

but there is a catch you can still change value of const variables if they are objects or arrays, const means that you can't change type of variable.

const a = {
   b: 'test 1'
};
a.b = 'test 2'  // this will work

:) hope this will help you.