Javascript scope


/ Published in: JavaScript
Save to your folder(s)

There are two scopes in JavaScript: global and function. If you don\'t declare a variable (using var), it assumes global. \r\n\r\nVariables declared in functions have function scope.


Copy this code and paste it in your HTML
  1. var jj = 0;
  2. function testFunc2() {
  3. jj = 42;
  4. //Don't declare your variables like this.
  5. //It is only an example to illustrate that variables declared anywhere in the function have function scope
  6. var jj;
  7. }
  8.  
  9. testFunc2(); alert(jj);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.