JS error 'redeclaration' of var when it's first line in program?
Asked Answered
S

9

13

SCRPT5039: Redeclaration of const property line 1 character 1

line1: var editObj = null;

This is the beginning of the file and I checked to make sure that variable is not in any other js files being called. Is it saying that I redeclare it later? (if so that line reference is not useful) or what is wrong with this?

Soppy answered 28/9, 2011 at 4:4 Comment(8)
Which browser or other Javascript engine? Where did you see this error? What is SCRPT5039?Gobbet
this says you have declared it before, check again..Caerleon
Just because it is the first line in that script doesn't matter if there are other scripts that are loaded before it.Abbieabbot
var alone will not do that, AFAIK. Something else is making the global property "const". Which browser/engine?Ezekielezell
it's in IE9 in the debug mode. no error being thrown in other browsers. I already ran 'find' through my whole project.. and it only returned occurrences in that fileSoppy
I fixed it by having var editObj on line 1 and putting editObj = null in the jQuery onLoad function (first thing that gets executed anyway). Still not seeing why it was an error in the first place, though. Or is it just one of those IE things...Soppy
tried with IE9 using var asdfasdfasdf = {}; Got the exact same error with debug on. Got the same error with asdfasdfasdf = null;.Icj
I get the exact same thing with my page. Every other browser is fine, including IE8. Hope somebody finds an answer!Pampuch
P
4

EDIT: Fixed it. For me, anyway. I got this error before the redeclaration error:

HTML1113: Document mode restart from Quirks to IE9 Standards 

This suggests that IE finds what it thinks is an error, so loads the page again in Quirks mode. Loading the page twice makes it think everything is declared twice. So the solution is to find what IE didn't like.

First I ran the page through the online HTML validator. Next I ran my javascript through jsLint. After all that, IE9 seemed happy. And as a bonus I have better quality code. I hope.

Pampuch answered 15/1, 2012 at 15:7 Comment(0)
E
6

I got this error with the following code:

var window;

I had added this declaration to workaround a node error when using the following code:

if (!window) {
  //node-specific stuff
}

Without the above declaration, node wold complain. In the end, I chose to copy/paste rather than try to share the exact same file between node and browser implementations.

Expunge answered 8/2, 2012 at 7:51 Comment(2)
For some reason ie does not support this. Very weirdAutarch
You might have figured it out, but I observed this kind of error is thrown when we use reserved key words as our variable name.Picador
P
4

EDIT: Fixed it. For me, anyway. I got this error before the redeclaration error:

HTML1113: Document mode restart from Quirks to IE9 Standards 

This suggests that IE finds what it thinks is an error, so loads the page again in Quirks mode. Loading the page twice makes it think everything is declared twice. So the solution is to find what IE didn't like.

First I ran the page through the online HTML validator. Next I ran my javascript through jsLint. After all that, IE9 seemed happy. And as a bonus I have better quality code. I hope.

Pampuch answered 15/1, 2012 at 15:7 Comment(0)
S
3

I had a similar problem with the same error, but my first line of code was an alert(0); which I had inserted to make sure the script was being loaded! Interestingly, the script was loaded according to IE9's developer tools, but the first line was never executed and the error pointed to this alert(0); as the redeclaration. I even inserted lines and spaces before it and the line and character number changed accordingly. However, this (obviously) wasn't the thing being redeclared because it's not even a declaration, let alone a redeclaration!

I deleted chunks from the end of the script until it executed the alert(0); (indicating that the script had loaded and been parsed successfully), and I discovered that the offending line was:

var screen;

Turns out IE9 already has a window.screen with which this declaration collided, and renaming my screen to eScreen fixed the problem.

So my answer is: don't trust IE9's indication of where the redeclaration is!

(It's also worth noting that the script ran fine in its original form on IE7, IE8 and IE10, just not on IE9.)

Strobel answered 7/6, 2013 at 12:7 Comment(1)
Indeed, and I notice that IE9's error is now "Redeclaration of const property", which is much more helpful.Strobel
P
1

I had the same problem in my code and it turn out that IE show wrong line were the redeclaration appear. In my case it was history I use later in the code. You should check whole code for redeclaration of the constants. You can try to comment out part of the code and see when it throw that error.

Pulpboard answered 6/3, 2013 at 9:53 Comment(0)
E
1

The error comes because you have declared some global/local variable which matches with default browser property. Something like

var window = '';
var navigator = '';

Try removing/commenting such declaration and see the effects.

Erbes answered 6/5, 2016 at 10:11 Comment(0)
M
1

I was getting this error too -- problem is the line it fires on is completely bogus. Like the OP, it was an early line in my script that was being flagged:

        var h_combinedView = true;

The error message is very misleading: "0x800a041c - JavaScript runtime error: Let/Const redeclaration"

The flagged line is not a const definition, and the value on it is defined once in my entire project and never again.

Eventually I tracked the problem down to an actual duplicated const definition.

const ve = { Normal: 'default', Search: 'search', View: 'view', Alts: 'ViewAlts', Edit: 'edit' }

(I had moved a definition used in multiple places into a shared file & forgot to remove one copy). The error message was legit -- it was a duplicated const definition -- but the line and identifier flagged had NOTHING to do with the problem.

Nothing like inaccurate error messages to force me to walk through my code.

:-)

Melissa answered 23/3, 2019 at 1:7 Comment(0)
I
0

I had the same error and came across this post. While I did not have the same root issue as the OP I thought I would share my solution in case others make my same mistake and arrive here. I received the error in a separate js file. After simplifying it, I found I could generate the error from the following code:

var foo = null;
var bar = null;
var localData = null;

The error said it was from the first line. However, foo was not being redeclared. The problem was that localData must have been used elsewhere (not in my code). No matter how far down in the file localData was declared, the error listed as being on the first real line of code.

So, if other solutions don't work, try renaming each variable in the code file to determine which one may be causing the problem. Don't trust that the debugger is pointing to the right line.

Encountered: HTML5/JS Window Store app.

Isolating answered 4/12, 2013 at 18:21 Comment(0)
H
0

From an error by me doing a git merge, I found that I had included the script in my HTML twice. This way, when it ran the script the second time, it redeclared the variable.

And it only gave me the error for the first const, which was confusing to me, as I tried to remove the variable or add another one to the file, it always gave me the error for the first line.

Hartill answered 28/3, 2018 at 9:18 Comment(0)
W
0

You'll see this if you're in browser and you accidentally declare the script source twice:

Eg:

<script src="app.js" defer></script>
<script src="app.js" defer></script>
Willock answered 27/5, 2024 at 19:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.