C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
.NET
ADO.NET
Android
ASP.NET
C#
Databases & DBA
Design Patterns & Practices
iOS
Java
OOP/OOD
SharePoint
Software Testing
Web Development
WPF
View All
2
Reply
What are global variables in javascript?
Mohan Gupta
12y
847
0
Reply
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Submit
Global variables are the variables declared outside a function. Global variables have global scope meaning all scripts and functions on the page can access them. The lifetime of a global variable starts with it's declaration and is deleted when the page is closed.Also, If you assign a value to a variable that has not been declared, it will automatically become a global variable, even if it is present inside a function.function GlobalEx() {// The variable greeting is not declared but a value is assigned.// So it will automatically become a global variablegreeting = "JavaScript Global variable example"; }GlobalEx();// Variable greeting is available outside the functionalert(greeting);
Praveen Dhatrika
10y
0
Hi Mohan ,var iAmGlobal = "some val"; //Global variable declaration//Any place in other part of codefunction doSomething() {//iAmGlobal = "changed value";alert(iAmGlobal); //I am accessible here too !! }
Amit Tripathi
11y
0
Message