JavaScript Local Scope

You are Here:

What is Scope?

Scope determines the availability (visibility, or accessibility) of variables.

Types of Scope

There are two types of scope available in JavaScript, they are

  • Global Scope
  • Local Scope

Local Scope

When a variable is declared inside a function, then the variable is said to be Local.

Local variable is accessible only within the function it is declared.

Note: The following example will throw an error, Please check your console while trying it.


HTML Online Editor
<!DOCTYPE html> <html> <body> <script> function myFunction(){ var txt = "Hello"; } myFunction(); // Error occurs console.log(txt); </script> </body> </html>

More Examples

Local variable is also accessible to its inner function.

HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point"></p> <script> var x = document.getElementById("point"); function myFunction(){ var txt = "Hello"; function displayText(){ x.innerText = txt; } displayText(); } myFunction(); </script> </body> </html>


Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author