- 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 scope of variables in JavaScript
The scope of a variable is the region of your program in which it is defined. JavaScript variables have only two scopes.
- Global Variables − A global variable has global scope which means it can be defined anywhere in your JavaScript code.
- Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable.
You can try to run the following code to learn how to work with 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
- What is the scope of local variables in Java?
- Scope of Variables in C#
- Scope of Variables in C++
- Scope of Variables in Python
- C++ Scope of Variables
- What are Local Scope Variables in Postman?
- Global Scope Variables in Postman?
- Scope and lifetime of variables in Java?
- What is the lifetime of JavaScript variables?
- What is the use of declaring variables in JavaScript?
- Variables, their types, and Scope in C++
- What do you mean by Scope of variables inside MySQL stored procedure?
- What is the best way of declaring multiple Variables in JavaScript?
- Access variables from parent scope in anonymous PHP function
- What is the use of scope resolution operator in C++?

Advertisements