2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244c0
title: Global vs. Local Scope in Functions
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/c2QwKH2'
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
It is possible to have both < dfn > local< / dfn > and < dfn > global< / dfn > variables with the same name. When you do this, the < code > local< / code > variable takes precedence over the < code > global< / code > variable.
In this example:
< blockquote > var someVar = "Hat";< br > function myFun() {< br > var someVar = "Head";< br > return someVar;< br > }< / blockquote >
The function < code > myFun< / code > will return < code > "Head"< / code > because the < code > local< / code > version of the variable is present.
< / section >
## Instructions
< section id = 'instructions' >
Add a local variable to < code > myOutfit< / code > function to override the value of < code > outerWear< / code > with < code > "sweater"< / code > .
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: Do not change the value of the global < code > outerWear</ code >
2018-10-20 21:02:47 +03:00
testString: assert(outerWear === "T-Shirt", 'Do not change the value of the global < code > outerWear< / code > ');
2018-10-04 14:37:37 +01:00
- text: < code > myOutfit</ code > should return < code > "sweater"</ code >
2018-10-20 21:02:47 +03:00
testString: assert(myOutfit() === "sweater", '< code > myOutfit< / code > should return < code > "sweater"< / code > ');
2018-10-04 14:37:37 +01:00
- text: Do not change the return statement
2018-10-20 21:02:47 +03:00
testString: assert(/return outerWear/.test(code), 'Do not change the return statement');
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// Setup
var outerWear = "T-Shirt";
function myOutfit() {
// Only change code below this line
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
// Only change code above this line
return outerWear;
}
myOutfit();
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
var outerWear = "T-Shirt";
function myOutfit() {
var outerWear = "sweater";
return outerWear;
}
```
< / section >