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:-
<!DOCTYPE html>
<script>
function car(){
window.carName = "Volvo";
function carDetails(){
alert(window.carName);
car();
carDetails();
</script>
</body>
</html>
Output
:
Do comment if you have any doubts and suggestion on this JavaScript variables topic.
Note:
The
All JS Examples codes
are tested on the Firefox browser and the Chrome browser.
OS:
Windows 10
Code: HTML 5 Version
Related