2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244bf
title: Local Scope and Functions
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cd62NhM'
2019-07-31 11:32:23 -07:00
forumTopicId: 18227
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
Variables which are declared within a function, as well as the function parameters have < dfn > local< / dfn > scope. That means, they are only visible within that function.
Here is a function < code > myTest< / code > with a local variable called < code > loc< / code > .
2019-05-17 06:20:30 -07:00
```js
function myTest() {
var loc = "foo";
console.log(loc);
}
myTest(); // logs "foo"
console.log(loc); // loc is not defined
```
2018-09-30 23:01:58 +01:00
< code > loc< / code > is not defined outside of the function.
< / section >
## Instructions
< section id = 'instructions' >
Declare a local variable < code > myVar< / code > inside < code > myLocalScope< / code > . Run the tests and then follow the instructions commented out in the editor.
< strong > Hint< / strong > < br > Refreshing the page may help if you get stuck.
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-11-27 02:57:38 -08:00
- text: The code should not contain a global < code > myVar</ code > variable.
2019-07-13 00:07:53 -07:00
testString: assert(typeof myVar === 'undefined');
2019-11-27 02:57:38 -08:00
- text: You should add a local < code > myVar</ code > variable.
2019-07-13 00:07:53 -07:00
testString: assert(/function\s+myLocalScope\s*\(\s*\)\s*\{\s[\s\S]+\s*var\s*myVar\s*(\s*|=[\s\S]+)\s*;[\s\S]+}/.test(code));
2019-02-05 14:40:58 +08:00
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function myLocalScope() {
2020-03-02 23:18:30 -08:00
'use strict';
// Only change code below this line
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
console.log(myVar);
}
myLocalScope();
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);
// Now remove the console log line to pass the test
```
< / div >
### Before Test
< div id = 'js-setup' >
```js
var logOutput = "";
var originalConsole = console
function capture() {
var nativeLog = console.log;
console.log = function (message) {
logOutput = message;
if(nativeLog.apply) {
nativeLog.apply(originalConsole, arguments);
} else {
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
nativeLog(nativeMsg);
}
};
}
function uncapture() {
console.log = originalConsole.log;
}
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
typeof myLocalScope === 'function' & & (capture(), myLocalScope(), uncapture());
(function() { return logOutput || "console.log never called"; })();
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function myLocalScope() {
'use strict';
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
var myVar;
console.log(myVar);
}
myLocalScope();
```
< / section >