Local variables are saved within a function using var for use exclusively within that function's scope, whereas global variables are declared outside of a function for accessible across the programme.
In JavaScript, global variables should be avoided or used sparingly. This is because other scripts can easily overwrite global variables. Global variables aren't necessarily bad or even dangerous, but they shouldn't overwrite the values of other variables.
One advantage is that they are simple to overwrite in various locations because they are widely available. Because global variables are window object properties, they can also overwrite anything in the window object. These are actual challenges that make our code difficult to understand.
var window. iAmGlobal = "some val" ; //Global variable declaration with window.
/In any other part of the code.
doSomething function ()
alert(window. iAmGlobal); /I am also available here!! /OR.
alert(iAmGlobal); /I am also available here!!
Learner's Ratings
4.8
Overall Rating
82%
17%
0%
1%
0%
Reviews
S
Sachin
5
most useful to begginar
M
Master Peace
5
best
S
Sunny
5
I really liked the way of explaining.
M
M Abdullah
5
Very Nice!
P
pratima nice mall
5
good explaining
B
Bikramjit singh
5
Very helpful to web development
M
Mohammad Arif
5
very helpful for beginners
A
Ankit Singh
5
good
R
Rajendra Deshmukh
5
Thank you for providing a valuable and well-managed session
Share a personalized message with your friends.