JSHint prevent redeclared global variable
up vote
0
down vote
favorite
I am using JSHint and I have a global variable (test) declared using the predef options. However in my code I can just override the global variable using
var test = 4;
Is there anyway to prevent this? I want to use my global variable without being able to override it in any way.
javascript jshint
|
show 1 more comment
up vote
0
down vote
favorite
I am using JSHint and I have a global variable (test) declared using the predef options. However in my code I can just override the global variable using
var test = 4;
Is there anyway to prevent this? I want to use my global variable without being able to override it in any way.
javascript jshint
3
Useconst
instead ofvar
.If your variable is a primitive, like number or string, it can't be overriden.
– HynekS
Nov 9 at 13:07
@HynekS It can't be overridden directly, but could still be overridden in scope, which I think the op might be trying to prevent. eg.const x = 1; { let x = 2; console.log(x); }
will still return 2.
– Keith
Nov 9 at 13:19
I think you have to refer this stackoverflow!
– user2623720
Nov 9 at 13:20
@Keith Well, but it's not the same x! (It's its evil twin brother..)
– HynekS
Nov 9 at 14:01
@HynekS, Yea Keith was right, I am aware how const works, but using const or var, my global variable test still gets overridden in the scope and I lose all previous data :(
– Wouter Klene
Nov 10 at 14:26
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am using JSHint and I have a global variable (test) declared using the predef options. However in my code I can just override the global variable using
var test = 4;
Is there anyway to prevent this? I want to use my global variable without being able to override it in any way.
javascript jshint
I am using JSHint and I have a global variable (test) declared using the predef options. However in my code I can just override the global variable using
var test = 4;
Is there anyway to prevent this? I want to use my global variable without being able to override it in any way.
javascript jshint
javascript jshint
asked Nov 9 at 13:06
Wouter Klene
1
1
3
Useconst
instead ofvar
.If your variable is a primitive, like number or string, it can't be overriden.
– HynekS
Nov 9 at 13:07
@HynekS It can't be overridden directly, but could still be overridden in scope, which I think the op might be trying to prevent. eg.const x = 1; { let x = 2; console.log(x); }
will still return 2.
– Keith
Nov 9 at 13:19
I think you have to refer this stackoverflow!
– user2623720
Nov 9 at 13:20
@Keith Well, but it's not the same x! (It's its evil twin brother..)
– HynekS
Nov 9 at 14:01
@HynekS, Yea Keith was right, I am aware how const works, but using const or var, my global variable test still gets overridden in the scope and I lose all previous data :(
– Wouter Klene
Nov 10 at 14:26
|
show 1 more comment
3
Useconst
instead ofvar
.If your variable is a primitive, like number or string, it can't be overriden.
– HynekS
Nov 9 at 13:07
@HynekS It can't be overridden directly, but could still be overridden in scope, which I think the op might be trying to prevent. eg.const x = 1; { let x = 2; console.log(x); }
will still return 2.
– Keith
Nov 9 at 13:19
I think you have to refer this stackoverflow!
– user2623720
Nov 9 at 13:20
@Keith Well, but it's not the same x! (It's its evil twin brother..)
– HynekS
Nov 9 at 14:01
@HynekS, Yea Keith was right, I am aware how const works, but using const or var, my global variable test still gets overridden in the scope and I lose all previous data :(
– Wouter Klene
Nov 10 at 14:26
3
3
Use
const
instead of var
.If your variable is a primitive, like number or string, it can't be overriden.– HynekS
Nov 9 at 13:07
Use
const
instead of var
.If your variable is a primitive, like number or string, it can't be overriden.– HynekS
Nov 9 at 13:07
@HynekS It can't be overridden directly, but could still be overridden in scope, which I think the op might be trying to prevent. eg.
const x = 1; { let x = 2; console.log(x); }
will still return 2.– Keith
Nov 9 at 13:19
@HynekS It can't be overridden directly, but could still be overridden in scope, which I think the op might be trying to prevent. eg.
const x = 1; { let x = 2; console.log(x); }
will still return 2.– Keith
Nov 9 at 13:19
I think you have to refer this stackoverflow!
– user2623720
Nov 9 at 13:20
I think you have to refer this stackoverflow!
– user2623720
Nov 9 at 13:20
@Keith Well, but it's not the same x! (It's its evil twin brother..)
– HynekS
Nov 9 at 14:01
@Keith Well, but it's not the same x! (It's its evil twin brother..)
– HynekS
Nov 9 at 14:01
@HynekS, Yea Keith was right, I am aware how const works, but using const or var, my global variable test still gets overridden in the scope and I lose all previous data :(
– Wouter Klene
Nov 10 at 14:26
@HynekS, Yea Keith was right, I am aware how const works, but using const or var, my global variable test still gets overridden in the scope and I lose all previous data :(
– Wouter Klene
Nov 10 at 14:26
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226290%2fjshint-prevent-redeclared-global-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Use
const
instead ofvar
.If your variable is a primitive, like number or string, it can't be overriden.– HynekS
Nov 9 at 13:07
@HynekS It can't be overridden directly, but could still be overridden in scope, which I think the op might be trying to prevent. eg.
const x = 1; { let x = 2; console.log(x); }
will still return 2.– Keith
Nov 9 at 13:19
I think you have to refer this stackoverflow!
– user2623720
Nov 9 at 13:20
@Keith Well, but it's not the same x! (It's its evil twin brother..)
– HynekS
Nov 9 at 14:01
@HynekS, Yea Keith was right, I am aware how const works, but using const or var, my global variable test still gets overridden in the scope and I lose all previous data :(
– Wouter Klene
Nov 10 at 14:26