/**
* Exercise:
*
* Function Scope
*
* Work in the console, or use "console.log()" to output to the console
* from the script
*
* Write two functions that share the same, global variable
* - Verify this by logging the value of the variable to the console
* from within each function
* Write two functions that share a non-global variable
* - Verify this by logging the value of the variable to the console
* from within each function
*/
//Part 1
var a = 5;
fun1();
fun2();
function fun1() {
console.log("fun1 : " + a);
return a;
}
function fun2() {
console.log("fun2 : " + a);
return a;
}
//Part 2
function fun3() {
var myVar = 7;
function fun4() {
myVar = 8;
console.log("fun4 : " + myVar);
return myVar;
}
console.log(fun4());
return myVar;
}
console.log(fun3());
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.