Jump to content

Recommended Posts

I am new to JS and am reading a book that says the following:
[code]
var iBaseNum = 10;

function addNumbers(iNum1, iNum2) {
  function doAddition() {
      return iNum1 + iNum2 + iBaseNum;
      }
  return doAddition();
}
[/code]

The book then says the variable iBaseNum is a global variable. My understanding is that to declare a global variable in ECMAScript, you leave off the 'var'. Can anyone explain this to me? Thank you.
Link to comment
https://forums.phpfreaks.com/topic/35006-global-variable-help/
Share on other sites

if you use the 'var' outside of any function, then the variable is global and it's current value can be accessed inside/outside any function.  If you use the 'var' inside of a function, then the variable is local to that function.
[code]var test = 10;
document.write test();  //prints 5 not 10!
document.write test2();  //prints 10

function test() {
    var test = 5;  //local variable, overrides gobal variable for this function only!
    return test; //returns local variable value
}
function test2() {
    return test; //returns global variable value
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/35006-global-variable-help/#findComment-165910
Share on other sites

Ok, so is there any reason to pass variables declared outside of a function into that function?? It seems to me that unless I am passing local variables from one function to another, I have no need to pass variables declared outside of functions since they are all global.

Moreover, everyone always screams that global variables are bad, bad, bad. How come every variable in JS declared outside of a function is global? Does the same hold true in Java, C++, Ruby, etc.?

Ryan
Link to comment
https://forums.phpfreaks.com/topic/35006-global-variable-help/#findComment-166392
Share on other sites

you have a need to pass variables declared globally in the parameters if you use the function with differents vars:
[code]function cleancode($html) {
    //a function to remove extraneous spaces, tabs, line feeds ect. from html code
    // I would call it with different strings 
    // fill in actual code
    return $cleanedcode;
}[/code]
who says you shouldn't use global vars?  Professors in invory towers? 
Link to comment
https://forums.phpfreaks.com/topic/35006-global-variable-help/#findComment-166604
Share on other sites

Are you saying that you shouldn't use global variables at all fenway because I've never heard that preached that much in articles I've read.  You could of course go overboard in usage of them but are you saying - no usage.  On my pages I use a few global vars that maintain the current state of the page and might be accessed in multiple functions in the page.  Those functions have parameters passed to them as well as accessing some global vars.  I could have passed the global vars as function parameters but using them globally just saves coding.  If global vars are bad them what is the suggested alternative?
Link to comment
https://forums.phpfreaks.com/topic/35006-global-variable-help/#findComment-167262
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.