2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244c0
2021-02-06 04:42:36 +00:00
title: Global vs. Local Scope in Functions
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/c2QwKH2'
forumTopicId: 18194
2021-01-13 03:31:00 +01:00
dashedName: global-vs--local-scope-in-functions
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
It is possible to have both < dfn > local</ dfn > and < dfn > global</ dfn > variables with the same name. When you do this, the `local` variable takes precedence over the `global` variable.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
In this example:
2020-04-29 18:29:13 +08:00
```js
var someVar = "Hat";
function myFun() {
var someVar = "Head";
return someVar;
}
```
2021-02-06 04:42:36 +00:00
The function `myFun` will return `"Head"` because the `local` version of the variable is present.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Add a local variable to `myOutfit` function to override the value of `outerWear` with `"sweater"` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should not change the value of the global `outerWear` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(outerWear === 'T-Shirt');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`myOutfit` should return `"sweater"` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(myOutfit() === 'sweater');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should not change the return statement.
2020-04-29 18:29:13 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(/return outerWear/.test(code));
2018-10-10 18:03:03 -04:00
```
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
// Setup
var outerWear = "T-Shirt";
function myOutfit() {
// Only change code below this line
// Only change code above this line
return outerWear;
}
myOutfit();
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
var outerWear = "T-Shirt";
function myOutfit() {
var outerWear = "sweater";
return outerWear;
}
```