2021-06-14 18:34:20 +00:00
---
id: 56533eb9ac21ba0edf2244c0
title: Ambito globale e ambito locale nelle funzioni
challengeType: 1
videoUrl: 'https://scrimba.com/c/c2QwKH2'
forumTopicId: 18194
dashedName: global-vs--local-scope-in-functions
---
# --description--
È possibile avere sia variabili < dfn > locali< / dfn > che < dfn > globali< / dfn > con lo stesso nome. Quando fai questo, la variabile locale ha la precedenza sulla variabile globale.
In questo esempio:
```js
2021-10-27 15:10:57 +00:00
const someVar = "Hat";
2021-06-14 18:34:20 +00:00
function myFun() {
2021-10-27 15:10:57 +00:00
const someVar = "Head";
2021-06-14 18:34:20 +00:00
return someVar;
}
```
La funzione `myFun` restituirà la stringa `Head` perché è presente la versione locale della variabile.
# --instructions--
Aggiungi una variabile locale alla funzione `myOutfit` per sovrascrivere il valore di `outerWear` con la stringa `sweater` .
# --hints--
Non dovresti cambiare il valore della variabile globale `outerWear` .
```js
assert(outerWear === 'T-Shirt');
```
`myOutfit` dovrebbe restituire la stringa `sweater` .
```js
assert(myOutfit() === 'sweater');
```
Non dovresti cambiare l'istruzione return.
```js
assert(/return outerWear/.test(code));
```
# --seed--
## --seed-contents--
```js
// Setup
2021-10-27 15:10:57 +00:00
const outerWear = "T-Shirt";
2021-06-14 18:34:20 +00:00
function myOutfit() {
// Only change code below this line
// Only change code above this line
return outerWear;
}
myOutfit();
```
# --solutions--
```js
2021-10-27 15:10:57 +00:00
const outerWear = "T-Shirt";
2021-06-14 18:34:20 +00:00
function myOutfit() {
2021-10-27 15:10:57 +00:00
const outerWear = "sweater";
2021-06-14 18:34:20 +00:00
return outerWear;
}
```