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
abstract | arguments | await | boolean |
break | byte | case | catch |
char | class | const | continue |
debugger | default | delete | do |
double | else | enum | eval |
export | extends | false | final |
finally | float | for | function |
goto | if | implements | import |
in | instanceof | int | interface |
let | long | native | new |
null | package | private | protected |
public | return | short | static |
super | switch | synchronized | this |
throw | throws | transient | true |
try | typeof | var | void |
volatile | while | with | yield |
JavaScript Objects, Methods, and Properties
We should avoid to use following JavaScript objects, properties, and methods name.
Array | Date | eval | function |
hasOwnProperty | Infinity | isFinite | isNaN |
isPrototypeOf | length | Math | NaN |
name | Number | Object | prototype |
String | toString | undefined | valueOf |
Miscellaneous Reserved Words
In addition to the above reserved words, we have to avoid the following identifiers as names of JavaScript variables.
alert | all | anchor | anchors |
area | assign | blur | button |
checkbox | clearInterval | clearTimeout | clientInformation |
close | closed | confirm | constructor |
crypto | decodeURI | decodeURIComponent | defaultStatus |
document | element | elements | embed |
embeds | encodeURI | encodeURIComponent | escape |
event | fileUpload | focus | form |
forms | frame | innerHeight | innerWidth |
layer | layers | link | location |
mimeTypes | navigate | navigator | frames |
frameRate | hidden | history | image |
images | offscreenBuffering | open | opener |
option | outerHeight | outerWidth | packages |
pageXOffset | pageYOffset | parent | parseFloat |
parseInt | password | pkcs11 | plugin |
prompt | propertyIsEnum | radio | reset |
screenX | screenY | scroll | secure |
select | self | setInterval | setTimeout |
status | submit | text | textarea |
top | unescape | untaint | window |