-1

I have a site with multiple Javascript files included. Two of the JS files have the following variable:

var $gridName

Will this cause any conflicts for me in my site? For example, will the value of $gridName be set by the last JS file that got included or some other type of unwanted behavior?

Here is the top of one of my files, where the variable is declared:

$(function () {
    var intlastRowId;
    var strAppName = 'allocation-details';
    var $gridName = $("#grid-allocation-details");
FastTrack
  • 8,810
  • 14
  • 57
  • 78
  • 2
    Does either script use a namespace? If not, then yes, there will be a conflict. – j08691 Oct 16 '14 at 17:52
  • @j08691 No, no namespaces are used. – FastTrack Oct 16 '14 at 17:53
  • The all-important question is: Are these *global* variables? If so, your scripts are likely going to clobber each other's state. If not, it doesn't matter. – user229044 Oct 16 '14 at 17:54
  • @meagar no, I do not believe they are global... They are declared as you see above: `var $gridName = "";` – FastTrack Oct 16 '14 at 17:55
  • 2
    @FastTrack using `var` doesn't make it not global in all cases. If you're executing code directly in the global scope, `var x` will still result in a global variable. – Kevin B Oct 16 '14 at 17:58
  • @KevinB see my edit above... would you say those variables have been declared in the global scope? – FastTrack Oct 16 '14 at 18:02
  • No, those are not in the global scope, and therefore will not conflict. – Kevin B Oct 16 '14 at 18:03
  • @KevinB ok, good, that was my intention! So being this is the way they are declared, these should not cause any conflict issues between one another. Is that correct? – FastTrack Oct 16 '14 at 18:05
  • @KevinB when I replied to your comment, you hadn't edited it yet. So all I saw was "No, those are not in the global scope." – FastTrack Oct 16 '14 at 18:09
  • Sorry, i have a bad habit of posting and editing, so much so that i often forget that i even do it. :) – Kevin B Oct 16 '14 at 18:10

1 Answers1

0

Yes same variable named may give conflicts i.e one value can overwrite other it depends on few things like are those variables global or local.

invinciblejai
  • 1,103
  • 1
  • 8
  • 15
  • These are not global variables.. They are declared as such: `var $gridName = "";` – FastTrack Oct 16 '14 at 17:56
  • @FastTrack Are they declared in the global scope? If so, they're global - it doesn't matter if they have `var` or not in that case – Ian Oct 16 '14 at 17:59
  • Pls check it http://stackoverflow.com/questions/944273/how-to-declare-a-global-variable-in-a-js-file – invinciblejai Oct 16 '14 at 18:01
  • Declaring variables with var doesn't make it local.if variable is declared with var inside scope of a function , then they are local to it.in your case if it's local than it should not crete issues and it depends also on which js is included before. – invinciblejai Oct 16 '14 at 18:03
  • @jai check my edit above... I show you where I declare my vars – FastTrack Oct 16 '14 at 18:05
  • Sorry @FastTrack , my mistake as it's in scope it should not create issues. Is it creating? – invinciblejai Oct 16 '14 at 18:09
  • No, it's not creating any issues, but the thought crossed my mind that it might, so I just wanted to verify if it would cause me problems or not. Thanks for the help – FastTrack Oct 16 '14 at 18:10