JAVASCRIPT

Variable Scope

Article by:
Date Published:
Last Modified:

Overview

Variable scope is a way to determining the lifespan of a variable in code.

Function Scope

Function scope is given to a JavaScript variable with the var keyword.

1
2
3
4
5
function myFunc() {
    var myVar = 8;
}

// myVar does not exist here

The above myVar variable cannot be accessed outside of the function myFunc().

Block Scope

As of EMCAScript 2015, JavaScript has block scoping capabilities. This is enabled with the introduction of the let keyword.


Authors

Geoffrey Hunter

Dude making stuff.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License .

Tags

    comments powered by Disqus