Course Content

  • 2.2 global_variable_with_window_object

Course Content


Local variables are saved within a function using var for use exclusively within that function's scope, whereas global variables are declared outside of a function for accessible across the programme.

In JavaScript, global variables should be avoided or used sparingly. This is because other scripts can easily overwrite global variables. Global variables aren't necessarily bad or even dangerous, but they shouldn't overwrite the values of other variables.

One advantage is that they are simple to overwrite in various locations because they are widely available. Because global variables are window object properties, they can also overwrite anything in the window object. These are actual challenges that make our code difficult to understand.

  • var window. iAmGlobal = "some val" ; //Global variable declaration with window.
  • /In any other part of the code.
  • doSomething function ()
  • alert(window. iAmGlobal); /I am also available here!! /OR.
  • alert(iAmGlobal); /I am also available here!!

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere