- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the lifetime of JavaScript variables?
The lifetime of a JavaScript variable begins when it is declared −
var rank;
A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.The completion of a function deletes the local variable.
A global variable has a global scope which means it can be defined anywhere in your JavaScript code. Global variables delete when the web browser is closed. However if a new page is loaded in the same browser window, then it remains.
Here’s the usage of global variables −
Example
You can try to run the following code to learn how to work with a scope of variables in JavaScript
<html> <body onload = checkscope();> <script> <!-- var myVar = "global"; // Declare a global variable function checkscope( ) { var myVar = "local"; // Declare a local variable document.write(myVar); } //--> </script> </body> </html>
- Related Articles
- Scope and lifetime of variables in Java?
- What is the scope of variables in JavaScript
- What is the use of declaring variables in JavaScript?
- What is the lifetime of a static variable in a C++ function?
- What is the best way of declaring multiple Variables in JavaScript?
- What is the difference between global and local Variables in JavaScript?
- What is the correct way to define global variables in JavaScript?
- What is the scope of local variables in Java?
- Can someone explain to me what the plus sign is before the variables in JavaScript?
- What is Binary Variables?
- What is the difference between dynamic type variables and object type variables?
- What is the difference between class variables and instance variables in Java?
- The following table gives the distribution of the lifetime of 400 neon lamps:
- Explain Lifetime of a variable in C language.
- What are the three books that everyone should read once in their lifetime?

Advertisements