Window Variable JavaScript | Declare global variables inside a function

Window Variable means that the variable is being declared at the global scope In JavaScript.

Window Variable is used to declare JavaScript global variables inside the function.

However, it’s not recommended to create or declarer Global variables generally and it should be avoided. A better way to define variables within the scope of functions.

Window Variable JavaScript Example code

A variable gets declared in the global scope window.carName = "Volvo"; can access this variable on the window object window.carName, because in the browser the ‘window’ object the global object.

HTML example code of JavaScript window object global variables:-

Output:

Declare global Window Variable JavaScript inside a function

Do comment if you have any doubts and suggestion on this JavaScript variables topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.