Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/basic-javascript/56533eb9ac21ba0edf2244c0.md
T
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

77 lines
1.4 KiB
Markdown

---
id: 56533eb9ac21ba0edf2244c0
title: Global vs. Local Scope in Functions
challengeType: 1
forumTopicId: 18194
dashedName: global-vs--local-scope-in-functions
---
# --description--
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.
In this example:
```js
const someVar = "Hat";
function myFun() {
const someVar = "Head";
return someVar;
}
```
The function `myFun` will return the string `Head` because the local version of the variable is present.
# --instructions--
Add a local variable to `myOutfit` function to override the value of `outerWear` with the string `sweater`.
# --hints--
You should not change the value of the global `outerWear`.
```js
assert(outerWear === 'T-Shirt');
```
`myOutfit` should return the string `sweater`.
```js
assert(myOutfit() === 'sweater');
```
You should not change the return statement.
```js
assert(/return outerWear/.test(__helpers.removeJSComments(code)));
```
# --seed--
## --seed-contents--
```js
// Setup
const outerWear = "T-Shirt";
function myOutfit() {
// Only change code below this line
// Only change code above this line
return outerWear;
}
myOutfit();
```
# --solutions--
```js
const outerWear = "T-Shirt";
function myOutfit() {
const outerWear = "sweater";
return outerWear;
}
```