Scope Using JavaScript

This is the same exercise looking at scope but using JavaScript to duplicate the VBScript exercise.

In this example the first document.write is done giving Frank. The second instance is in the local function in the code block. I added some methods so that you could better visualize what was happening. I added a document.write(dummy) after the first document.write and return name inside the function so it would return that value to dummy.

The Code:

<script type="text/javascript">
	var name, dummy;         //global variable
	name = "Frank";
	document.write(name);    //does the first write
	
	dummy=someFunction();   //calls someFunction jump to codeblock

	document.write(name);	//third write is Frank
	document.write(dummy);	//does fourth write of Richard
	
	function someFunction()  //start code block
	{
	var name;  				//declare the variable
	
	name="Richard";			//sets name to Richard

	document.write(name);   //writes second name of richard
	return name;			//puts richard into dummy thru someFunction
	}						//goes back up to third write
	
</script>