--- id: 56533eb9ac21ba0edf2244be title: Global Scope and Functions challengeType: 1 videoUrl: '' localeTitle: Âmbito global e funções --- ## Description
Em JavaScript, o escopo se refere à visibilidade das variáveis. Variáveis ​​definidas fora de um bloco de funções possuem escopo Global . Isso significa que eles podem ser vistos em qualquer lugar no seu código JavaScript. As variáveis ​​que são usadas sem a palavra-chave var são criadas automaticamente no escopo global . Isso pode criar consequências indesejadas em outro lugar no seu código ou ao executar uma função novamente. Você deve sempre declarar suas variáveis ​​com var .
## Instructions
Usando var , declare uma variável global myGlobal fora de qualquer função. Inicialize com um valor de 10 . Dentro da função fun1 , atribua 5 a oopsGlobal sem usar a palavra-chave var .
## Tests
```yml tests: - text: myGlobal deve ser definido testString: 'assert(typeof myGlobal != "undefined", "myGlobal should be defined");' - text: myGlobal deve ter um valor de 10 testString: 'assert(myGlobal === 10, "myGlobal should have a value of 10");' - text: myGlobal deve ser declarado usando a palavra-chave var testString: 'assert(/var\s+myGlobal/.test(code), "myGlobal should be declared using the var keyword");' - text: oopsGlobal deve ser uma variável global e ter um valor de 5 testString: 'assert(typeof oopsGlobal != "undefined" && oopsGlobal === 5, "oopsGlobal should be a global variable and have a value of 5");' ```
## Challenge Seed
```js // Declare your variable here function fun1() { // Assign 5 to oopsGlobal Here } // Only change code above this line function fun2() { var output = ""; if (typeof myGlobal != "undefined") { output += "myGlobal: " + myGlobal; } if (typeof oopsGlobal != "undefined") { output += " oopsGlobal: " + oopsGlobal; } console.log(output); } ```
### Before Test
```js var logOutput = ""; var originalConsole = console function capture() { var nativeLog = console.log; console.log = function (message) { logOutput = message; if(nativeLog.apply) { nativeLog.apply(originalConsole, arguments); } else { var nativeMsg = Array.prototype.slice.apply(arguments).join(' '); nativeLog(nativeMsg); } }; } function uncapture() { console.log = originalConsole.log; } var oopsGlobal; capture(); ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```