Create a code with three level of execution context in JavaScript

Description

The following code shows how to create a code with three level of execution context.

Example


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var name = "JavaScript";
function changeName(){<!--from w ww  . j  a va2  s. co  m-->
var anotherName = "HTML";
function swapNames(){
var tempName = anotherName;
document.writeln("tempName:"+tempName);
anotherName = name;
name = tempName;
//name, anotherName, and tempName
//are all accessible here
}
//name and anotherName are accessible here,
//but not tempName
swapNames();
}
//only name is accessible here
changeName();
document.writeln(name);//HTML

</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Create a code with three level of execution context in JavaScript
Home »
  Javascript Tutorial »
    Data Type »
      Variable
Javascript Tutorial Variable
Copy reference during assignment for object...
Copy value from one primitive type to anoth...
Create a code with three level of execution...
Define a variable outside a function and ca...
Demonstrate that there are no Block-Level S...
Demonstrate that variable defined with var ...
Demonstrate that variable defined without v...
Demonstrate that variables created by the v...
Find out the difference between primitive w...
Use eval to evaluate dynamic expressions in...
Use typeof operator to check if a variable ...