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
2021-01-13 03:31:00 +01:00
dashedName: local-scope-and-functions
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2021-03-02 16:12:12 -08:00
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.
2020-11-27 19:02:05 +01:00
Here is a function `myTest` with a local variable called `loc` .
2019-05-17 06:20:30 -07:00
```js
function myTest() {
2021-10-26 01:55:58 +09:00
const loc = "foo";
2019-05-17 06:20:30 -07:00
console.log(loc);
}
2021-10-26 01:55:58 +09:00
2021-03-02 16:12:12 -08:00
myTest();
console.log(loc);
2019-05-17 06:20:30 -07:00
```
2022-02-12 01:38:46 +05:30
The `myTest()` function call will display the string `foo` in the console. The `console.log(loc)` line (outside of the `myTest` function) will throw an error, as `loc` is not defined outside of the function.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --instructions--
2020-04-29 13:09:48 +02:00
2020-11-27 19:02:05 +01:00
The editor has two `console.log` s to help you see what is happening. Check the console as you code to see how it changes. Declare a local variable `myVar` inside `myLocalScope` and run the tests.
2020-04-29 13:09:48 +02:00
2021-03-02 16:12:12 -08:00
**Note:** The console will still display `ReferenceError: myVar is not defined` , but this will not cause the tests to fail.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2019-02-05 14:40:58 +08:00
2020-11-27 19:02:05 +01:00
The code should not contain a global `myVar` variable.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function declared() {
myVar;
}
2021-10-26 01:55:58 +09:00
2020-11-27 19:02:05 +01:00
assert.throws(declared, ReferenceError);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
You should add a local `myVar` variable.
```js
assert(
2021-04-28 16:18:54 +01:00
/functionmyLocalScope\(\)\{.*(var|let|const)myVar[\s\S]*}/.test(
2020-11-27 19:02:05 +01:00
__helpers.removeWhiteSpace(code)
)
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
function myLocalScope() {
2020-03-02 23:18:30 -08:00
// Only change code below this line
2018-10-08 01:01:53 +01:00
2020-04-29 13:09:48 +02:00
console.log('inside myLocalScope', myVar);
2018-09-30 23:01:58 +01:00
}
myLocalScope();
// Run and check the console
// myVar is not defined outside of myLocalScope
2020-04-29 13:09:48 +02:00
console.log('outside myLocalScope', myVar);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function myLocalScope() {
2020-04-29 13:09:48 +02:00
// Only change code below this line
2021-10-26 01:55:58 +09:00
let myVar;
2020-04-29 13:09:48 +02:00
console.log('inside myLocalScope', myVar);
2018-09-30 23:01:58 +01:00
}
myLocalScope();
2020-04-29 13:09:48 +02:00
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);
2018-09-30 23:01:58 +01:00
```