Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Javascript Scope: Example

JavaScript has function scope and global scope. Variables declared within a function are local to that function, while variables declared outside a function are global and accessible everywhere. If a variable is assigned a value without being declared, it will become a global variable. The lifetime of local variables is the duration of the function call, while global variables exist for the lifetime of the page.

Uploaded by

Alex Mociran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Javascript Scope: Example

JavaScript has function scope and global scope. Variables declared within a function are local to that function, while variables declared outside a function are global and accessible everywhere. If a variable is assigned a value without being declared, it will become a global variable. The lifetime of local variables is the duration of the function call, while global variables exist for the lifetime of the page.

Uploaded by

Alex Mociran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaScript Scope

In JavaScript, objects and functions are also variables.


In JavaScript, scope is the set of variables, objects, and functions you
have access to.
JavaScript has function scope: The scope changes inside functions.

Local JavaScript Variables


Variables declared within a JavaScript function, become LOCAL to the function.
Local variables have local scope: They can only be accessed within the
function.

Example
// code here can not use carName
function myFunction() {
var carName = "Volvo";
// code here can use carName
}
Since local variables are only recognized inside their functions, variables with
the same name can be used in different functions.
Local variables are created when a function starts, and deleted when the
function is completed.

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.

Example
var carName = " Volvo";
// code here can use carName
function myFunction() {
// code here can use carName
}

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 carName as a global variable, even if it is
executed inside a function.

Example
// code here can use carName
function myFunction() {
carName = "Volvo";
// code here can use carName
}

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.

Global variables are deleted when you close the page.

Function Arguments
Function arguments (parameters) work as local variables inside functions.

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.

Example
// code here can use window.carName
function myFunction() {
carName = "Volvo";
}

You might also like