2021-02-06 04:42:36 +00:00
---
id: 56533eb9ac21ba0edf2244be
2021-03-10 08:16:44 -07:00
title: Ámbito global y funciones
2021-02-06 04:42:36 +00:00
challengeType: 1
videoUrl: 'https://scrimba.com/c/cQM7mCN'
forumTopicId: 18193
dashedName: global-scope-and-functions
---
# --description--
2021-03-10 08:16:44 -07:00
En JavaScript, el < dfn > ámbito< / dfn > se refiere a la visibilidad de las variables. Las variables definidas fuera de un bloque de función tienen un ámbito < dfn > Global< / dfn > . Esto significa que pueden ser observadas desde cualquier lugar en tu código JavaScript.
2021-02-06 04:42:36 +00:00
2021-10-31 23:08:44 -07:00
Las variables que se declaran sin las palabras clave `let` o `const` se crean automáticamente en el ámbito `global` . Esto puede crear consecuencias no intencionadas en cualquier lugar de tu código o al volver a ejecutar una función. Siempre debes declarar tus variables con `let` o `const` .
2021-02-06 04:42:36 +00:00
# --instructions--
2021-10-31 23:08:44 -07:00
Usando `let` o `const` , declara una variable global llamada `myGlobal` fuera de cualquier función. Inicialízala con un valor de `10` .
2021-02-06 04:42:36 +00:00
2021-10-31 23:08:44 -07:00
Dentro de la función `fun1` , asigna `5` a `oopsGlobal` ** *sin*** usar las palabras clave `let` o `const` .
2021-02-06 04:42:36 +00:00
# --hints--
2021-03-10 08:16:44 -07:00
`myGlobal` debe ser definida
2021-02-06 04:42:36 +00:00
```js
assert(typeof myGlobal != 'undefined');
```
2021-03-10 08:16:44 -07:00
`myGlobal` debe tener un valor de `10`
2021-02-06 04:42:36 +00:00
```js
assert(myGlobal === 10);
```
2021-10-31 23:08:44 -07:00
`myGlobal` debe declararse usando las palabras clave `let` o `const`
2021-02-06 04:42:36 +00:00
```js
2021-10-31 23:08:44 -07:00
assert(/(let|const)\s+myGlobal/.test(code));
2021-02-06 04:42:36 +00:00
```
2021-03-10 08:16:44 -07:00
`oopsGlobal` debe ser una variable global y tener un valor de `5`
2021-02-06 04:42:36 +00:00
```js
assert(typeof oopsGlobal != 'undefined' & & oopsGlobal === 5);
```
# --seed--
## --before-user-code--
```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-user-code--
```js
fun1();
fun2();
uncapture();
(function() { return logOutput || "console.log never called"; })();
```
## --seed-contents--
```js
// Declare the myGlobal variable below this line
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);
}
```
# --solutions--
```js
2021-10-31 23:08:44 -07:00
const myGlobal = 10;
2021-02-06 04:42:36 +00:00
function fun1() {
oopsGlobal = 5;
}
function fun2() {
var output = "";
if(typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if(typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}
```