Files
freeCodeCamp/curriculum/challenges/portuguese/02-javascript-algorithms-and-data-structures/basic-javascript/global-vs.-local-scope-in-functions.md

79 lines
1.4 KiB
Markdown
Raw Normal View History

---
id: 56533eb9ac21ba0edf2244c0
title: Diferenciar escopo global e local em funções
challengeType: 1
videoUrl: 'https://scrimba.com/c/c2QwKH2'
forumTopicId: 18194
dashedName: global-vs--local-scope-in-functions
---
# --description--
É possível ter as variáveis <dfn>local</dfn> e <dfn>global</dfn> com o mesmo nome. Quando você faz isso, a variável local tem precedência sobre a variável global.
2021-07-09 21:23:54 -07:00
Neste exemplo:
```js
var someVar = "Hat";
function myFun() {
var someVar = "Head";
return someVar;
}
```
2021-07-09 21:23:54 -07:00
A função `myFun` retornará a string `Head` porque a versão local da variável está presente.
# --instructions--
2021-07-09 21:23:54 -07:00
Adicione uma variável local para a função `myOutfit` para sobrescrever o valor de `outerWear` com a string `sweater`.
# --hints--
2021-07-09 21:23:54 -07:00
Você não deve alterar o valor da variável global `outerWear`.
```js
assert(outerWear === 'T-Shirt');
```
2021-07-09 21:23:54 -07:00
`myOutfit` deve retornar a string `sweater`.
```js
assert(myOutfit() === 'sweater');
```
2021-07-09 21:23:54 -07:00
Você não deve alterar a instrução de retorno.
```js
assert(/return outerWear/.test(code));
```
# --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();
```
# --solutions--
```js
var outerWear = "T-Shirt";
function myOutfit() {
var outerWear = "sweater";
return outerWear;
}
```