This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here!
Smartphone icons created by Freepik - Flaticon
7.9 JS Scope
Scope determines the accessibility (visibility) of variables.
JavaScript Function Scope
In JavaScript there are two types of scope:
JavaScript has function scope: Each function creates a new scope.
Scope determines the accessibility (visibility) of these variables.
Variables defined inside a function are not accessible (visible) from outside the function.
Local JavaScript Variables
Variables declared within a JavaScript function, become LOCAL to the function.
Local variables have Function scope: They can only be accessed from within the function.
// code here can NOT use carName
function myFunction() {
var carName = "Volvo";
// code here CAN use carName
}
Global JavaScript Variables
A variable declared outside a function, becomes GLOBAL .
A global variable has global scope : All scripts and functions on a web page can access it.
var carName = "Volvo";
// code here can use carName
function myFunction() {
// code here can also use carName
}
JavaScript Variables
In JavaScript, objects and functions are also variables.
Scope determines the accessibility of variables, objects, and functions from different parts of the code.
Automatically Global
If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.
This code example will declare a global variable carName , even if the value is assigned inside a function.
myFunction();
// code here can use carName
function myFunction() {
carName = "Volvo";
}
Strict Mode
All modern browsers support running JavaScript in "Strict Mode".
You will learn more about how to use strict mode in a later chapter of this tutorial.
In "Strict Mode", undeclared variables are not automatically global.
Global Variables in HTML
With JavaScript, the global scope is the complete JavaScript environment.
In HTML, the global scope is the window object. All global variables belong to the window object.
var carName = "Volvo";
// code here can use window.carName
Warning
Do NOT create global variables unless you intend to.
Your global variables (or functions) can overwrite window variables (or functions). Any function, including the window object, can overwrite your global variables and functions.
The Lifetime of JavaScript Variables
The lifetime of a JavaScript variable starts when it is declared.
Local variables are deleted when the function is completed.
In a web browser, global variables are deleted when you close the browser window (or tab).
Function Arguments
Function arguments (parameters) work as local variables inside functions.
JavaScript icons used in the buttons provided by ICONS8.COM . Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Sets Map Objects part 1
JS Sets Map Objects part 2
JS Sets Map Objects part 3
JS Sets Map Objects part 4
JS Sets Map Objects part 5
JS Sets Delete Map Objects
JS Sets Map.has() part 1
JS Sets Map.has() part 2
JS Sets Map Objects forEach()
JS Sets Map Objects Map.entries()
JS Map Objects
JS Map Objects - Map.set() part 1
JS Map Objects - Map.set() part 2
JS Map Objects Map.get()
JS Map Objects - Map.size
JS Map Objects - Map.delete()
JS Map Objects - Map.has() part 1
JS Map Objects - Map.has() part 2
JS Map Objects - Map.forEach()
JS Map Objects - Map.entries()
JS typeof Operator part 1
JS typeof Operator part 2
JS typeof Operator part 3
JS typeof Properties - constructor Property part 1
JS typeof Properties - constructor Property part 2
JS typeof Properties - Array Object
JS typeof Operator - Date Object
JS typeof Operator - Date Function
JS typeof Operator - undefined part 1
JS typeof Operator - undefined part 2
JS typeof Operator - empty values
JS typeof Operator - null
JS typeof Operator - undefined Data Type
JS instanceof Operator
JS void Operator
JS Operators - typeof Operator
JS Properties - constructor Property
JS Arrays - isArray() function part 1
JS Arrays - isArray() function part 2
JS Date Object - isDate() function part 1
JS Date Object - isDate() function part 2
JS Converting Numbers to Strings part 1
JS Converting Numbers to Strings part 2
JS typeof Operator part 1
JS typeof Operator part 2
JS Bitwise AND
JS Bitwise OR
JS Bitwise XOR
JS Bitwise NOT
JS Bitwise Left
JS Bitwise Right
JS Unsigned Bitwise Right
JS Convert Decimal to Binary
JS Convert Binary to Decimal
JS String Methods
JS Regular Expressions part 1
JS String Methods - String replace()
JS Regular Expressions part 2
JS Regular Expressions part 3
JS Error Handling part 1
JS try catch
JS Errors - RangeError
JS Errors - ReferenceError
JS Errors - SyntaxError
JS Errors - TypeError
JS Errors - URIError
JS Scope - Local Variables
JS Scope - GLOBAL Variable
JS Global Variables
JS Scope - Global HTML variables
JS Hoisted Declarations part 1
JS Hoisted Declarations part 2
JS Hoisting part 1
JS Hoisting part 2
JS Initializations are Not Hoisted part 1
JS Initializations are Not Hoisted part 2
JS Initializations are Not Hoisted part 3
JS "use strict" Mode part 1
JS "use strict" Mode part 2
JS this Keyword
JS this Keyword [object Window] part 1
JS this Keyword - window object
JS this Keyword [object Window] part 2
JS this Keyword - Function
JS this Keyword - Event Handler
JS this Keyword - Event Handler
JS this Keyword - person object part 1
JS this Keyword - person object part 2
JS this Keyword - Explicit Function Binding
JS this Keyword - Function bind()
JS this Keyword - Arrow Function part 1
JS this Keyword - Arrow Function part 2