0

I have a line in my source code written by someone else:

var campaignLimits = 10, campaignsArray = new Array();

I just wanted to know, whether campaignsArray here becomes global variable, or the var applies to campaignsArray as well?

9
  • 1
    is it written inside a function ? Commented Nov 30, 2016 at 8:28
  • 2
    var campaignLimits = 10, campaignsArray = new Array(); is the same as var campaignLimits = 10; var campaignsArray = new Array();. Whether they're global or not will depend on the scope where they're defined. Commented Nov 30, 2016 at 8:28
  • the second one. for testing you can put it in a function and console.log that variable outside of that function. Commented Nov 30, 2016 at 8:29
  • var applies to campaignsArray as well. It's supported by Douglas Crockford, you may find some articles on this choice : sixrevisions.com/javascript/single-var-pattern and its critic danhough.com/blog/single-var-pattern-rant Commented Nov 30, 2016 at 8:29
  • Both campaignLimits and campaignArrays become a variable within a current scope. If you declare it in a global scope, they will both become global variables. Commented Nov 30, 2016 at 8:29

1 Answer 1

0

Assuming you have not used any programming pattern, If its written inside a function then its not global.

(function() { var campaignLimits = 10, campaignsArray = new Array(); })();

as @phoa commented it is same as

(function() { var campaignLimits = 10; var campaignsArray = new Array(); })();

Try it in your console and see whether you will be able to access campaignsArray.

Sign up to request clarification or add additional context in comments.

1 Comment

What do you mean by "assuming you have not used any programming pattern"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.