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'
2019-07-31 11:32:23 -07:00
forumTopicId: 18194
2021-01-13 03:31:00 +01:00
dashedName: global-vs--local-scope-in-functions
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2021-06-12 20:53:45 +02: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-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
In this example:
2019-05-17 06:20:30 -07:00
```js
2021-10-26 01:55:58 +09:00
const someVar = "Hat";
2019-05-17 06:20:30 -07:00
function myFun() {
2021-10-26 01:55:58 +09:00
const someVar = "Head";
2019-05-17 06:20:30 -07:00
return someVar;
}
```
2021-06-12 20:53:45 +02:00
The function `myFun` will return the string `Head` because the local version of the variable is present.
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2021-03-02 16:12:12 -08:00
Add a local variable to `myOutfit` function to override the value of `outerWear` with the string `sweater` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You should not change the value of the global `outerWear` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(outerWear === 'T-Shirt');
2018-09-30 23:01:58 +01:00
```
2021-03-02 16:12:12 -08:00
`myOutfit` should return the string `sweater` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(myOutfit() === 'sweater');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You should not change the return statement.
```js
assert(/return outerWear/.test(code));
```
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
// Setup
2021-10-26 01:55:58 +09:00
const outerWear = "T-Shirt";
2018-09-30 23:01:58 +01:00
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();
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2021-10-26 01:55:58 +09:00
const outerWear = "T-Shirt";
2018-09-30 23:01:58 +01:00
function myOutfit() {
2021-10-26 01:55:58 +09:00
const outerWear = "sweater";
2018-09-30 23:01:58 +01:00
return outerWear;
}
```