global variable in java script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhishekbrave
    New Member
    • Dec 2007
    • 79

    global variable in java script

    what is the difference between declaring variable with var or without var,which one is global

    Code:
    var i //is it a global
          /
    j     //or is this a global
  • vikas251074
    New Member
    • Dec 2007
    • 198

    #2
    Hello,
    Any variable that is initialized inside a function using the var keyword will have a local scope. If a variable is initialized inside a function without var, it will have a global scope. A local variable can have the same name as a global variable.
    i.e. If you declare a variable inside function without using var keyword, then it will be a global variable. That it the difference.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      in case you use FF/Moz in javascript-strict mode then the error console will give you a warning when you use undeclared variables ... the above explainations are correct but i want to add: never ever rely on that mechanism in a way to just not declare variables that you want to have in the window scope (which actually is the global scope) ... ALWAYS declare variables!!!

      kind regards

      Comment

      Working...