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.










share|improve this question


















  • 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












  • @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















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.










share|improve this question


















  • 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












  • @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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 13:06









Wouter Klene

1




1








  • 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












  • @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




    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










  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Android Play Services Check

Where to put API Key in Google Cloud Vision for PHP