Local & Global Variable in Javascript


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



Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2.  
  3. var MyVariable = 'global';
  4.  
  5. function x()
  6. {
  7. var MyVariable = 'local';
  8. alert('x: ' + MyVariable);
  9. }
  10.  
  11. function y()
  12. {
  13. alert('y: ' + MyVariable);
  14. }
  15.  
  16.  
  17. x();
  18. y();
  19. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.