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
80%
18%
0%
2%
0%
Reviews
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
S
SHIROYA RAJAN
5
nice cource
A
Abhishek Kumar
5
Best platform of learning.
A
Anmol
5
Best courses ever.True words
T
tejas jagtap
5
best
A
Anmol
5
I have completed this course . This is the best course to learn javascript with easy explanation. Thanks LearnVern for this course.
Share a personalized message with your friends.