JavaScript – Global Variables


What is the Global variable in JavaScript ?

Global scope variables can be defined anywhere inside the JavaScript code.

The global scope variable can be accessed from anywhere inside the javascript code.

Example :

<!DOCTYPE html>
<html>
        <head>
        <title>Our Title</title>
        <script>
                //global variable
                var b = 20;

                //global function
                function a(){
                console.log(b);  //access global variable
                }
        </script>
        </head>
        <body>
        </body>
</html>

Output :

20

How to create Automatically Global variable ?

We can assign a global variable automatically by

Example :

<!DOCTYPE html>
<html>
	<head>
	<title>Our title</title>
	</head>
	<script>
		myFunction();
		document.write("My Name is " + Name);
		function myFunction() {
		Name = "Abdul Kalam";
		}
	</script>
	<body>
	</body>
</html>

Output :

My Name is Abdul Kalam

Javascript advanced course !!!

Click to Learn More about – Javascript online learning

What are the advantages of global variable ?

Global variables can be accessed anywhere inside the javascript code.

Global variables can be modified easily to get reflected in the whole javascript code.

What are the disadvantages of global variable ?


Global variables cannot be created automatically when the browser is in “Strict Mode”.

Global variables/functions can overwrite local variables/functions.


Local variables can also overwrite our global variables and functions.

What is the lifetime of JavaScript Variables ?

JavaScript variables lifetime starts when it is created/declared.

Local variables lifetime ends when the function call is completed.

Global variables lifetime ends when we close the browser window, It remains available when it again loaded.

What are the JavaScript reserved keywords ?

Some of the reserved keywords which cannot be used as JavaScript variables are listed below.

JavaScript Reserved Keywords

abstractargumentsawaitboolean
breakbytecasecatch
charclassconstcontinue
debuggerdefaultdeletedo
doubleelseenumeval
exportextendsfalsefinal
finallyfloatforfunction
gotoifimplementsimport
ininstanceofintinterface
letlongnativenew
nullpackageprivateprotected
publicreturnshortstatic
superswitchsynchronizedthis
throwthrowstransienttrue
trytypeofvarvoid
volatilewhilewithyield

JavaScript Objects, Methods, and Properties

We should avoid to use following JavaScript objects, properties, and methods name.

ArrayDateevalfunction
hasOwnPropertyInfinityisFiniteisNaN
isPrototypeOflengthMathNaN
nameNumberObjectprototype
StringtoStringundefinedvalueOf

Miscellaneous Reserved Words

In addition to the above reserved words, we have to avoid the following identifiers as names of JavaScript variables.

alertallanchoranchors
areaassignblurbutton
checkboxclearIntervalclearTimeoutclientInformation
closeclosedconfirmconstructor
cryptodecodeURIdecodeURIComponentdefaultStatus
documentelementelementsembed
embedsencodeURIencodeURIComponentescape
eventfileUploadfocusform
formsframeinnerHeightinnerWidth
layerlayerslinklocation
mimeTypesnavigatenavigatorframes
frameRatehiddenhistoryimage
imagesoffscreenBufferingopenopener
optionouterHeightouterWidthpackages
pageXOffsetpageYOffsetparentparseFloat
parseIntpasswordpkcs11plugin
promptpropertyIsEnumradioreset
screenXscreenYscrollsecure
selectselfsetIntervalsetTimeout
statussubmittexttextarea
topunescapeuntaintwindow