Global variables in JavaScript
Oct 22, 2020 09:11 0 Comments Javascript PARTH

                Global Variables in JavaScript

JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function.

Let’s see the simple example of global variable in JavaScript.

 

Declaring JavaScript global variable within function

To declare JavaScript global variables inside function, you need to use window object. For example:

window.value=90;  

Now it can be declared inside any function and can be accessed from any function. For example:

function m(){  

window.value=100;//declaring global variable by window object  

}  

function n(){  

alert(window.value);//accessing global variable from other function  

}  

Internals of global variable in JavaScript

When you declare a variable outside the function, it is added in the window object internally. You can access it through window object also. For example:

var value=50;  

function a(){  

alert(window.value);//accessing global variable   

}  

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect