0

Does anyone knows why Firefox keeps giving me this redeclaration of const x error?

In the console of Firefox, I wrote the follow code. However when I run it the first time it was fine. It is NOT fine the 2nd time or 3rd time and subsequent time. What is not fine is that it give me an error redeclaration of const x. I don't know why because I am not re-declaring x. For context, I am still trying to study Javascript for which it is driving me crazy.

const x = 5;

console.log(x*8);

result of running following code

I am so frustrated I want to pull all my hair out. Anyway, I was following the youtube video that recommended that to follow along, I should use Firefox. After fiddling with it for awhile, I manually refresh the page and re-run the code. After refresh the page each time I run the code, I did not get an error. However, my question is why do I have to manually refresh the page for it to work everytime? Is there an auto refresh? Does it only give me an error in const variable? Is this a bug or is this normal for Firefox?

Tkhuu
  • 1
  • @tikhu, code executed twice. first time is declared and variable stored so second time throwing Syntax error "redeclaration of const x" error. this is not firefox specific issue. its expected ! – Venkat.R Dec 25 '22 at 20:22
  • "*I am not re-declaring `x`*" - well yes you are, when you are running the code again in the same (global) scope! – Bergi Dec 25 '22 at 21:55
  • If you really need to run the code multiple times, place in in a block scope (`{ const x = 5; console.log(x*8); }`) which declares `x` only inside that block - creating a new variable every time you run it. It won't be reachable from further statements in the console. – Bergi Dec 25 '22 at 21:58

2 Answers2

1

I went through that too when I was learning. The reason why is that your variable is being saved locally. Meaning you don't need to declare it again every time. For test you can declare it press enter and then console.log(x).

The reason why it works when refreshing it. Is because the variable is only being saved locally. Upon refresh you are resetting it to the new server response.

codecombs
  • 13
  • 5
0

It's not just Firefox, other browsers may behave that way - for example, Chrome did until a few years ago.

The problem is that everything you enter into the browser console is interpreted as happening in global scope, and when you declare const x = 5; and then do so again a little later in the same "session" (without refreshing the page), it's just as if you were executing those statements successively in an actual script. That is, it's as if you were executing a script from a .js file which went like this:

const x = 5;

console.log(x*8);

const x = 5;

console.log(x*8);

Obviously executing such a script would result in a "redeclaration of const" error, and the same happens in the console for the reasons I've described.

To avoid this, either choose a new variable name, or use var instead of const. Or use let and change the value with x = 6 or whatever, without using const the second time. Likely at least one of these will allow you to do whatever it is you're trying to do.

Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34
  • "*Chrome also behaves this way*" - actually it doesn't when you use the devtools console – Bergi Dec 25 '22 at 21:56
  • hmm it seems you're right, maybe this is a recent change as Chrome is the browser I use 99% of the time and I'm sure I've observed similar behaviour in the console with it in the past. – Robin Zigmond Dec 25 '22 at 22:03
  • Oh how time flies! The feature [was introduced with Chrome 80](https://developer.chrome.com/blog/new-in-devtools-80/#redeclarations) just three years ago :-) – Bergi Dec 25 '22 at 22:05
  • lol. But it's entirely possible I am indeed remembering behaviour from over 3 years ago. Thanks, I'll edit the answer. – Robin Zigmond Dec 25 '22 at 22:17