Skip to content

Instantly share code, notes, and snippets.

@johnsi15
Last active June 16, 2017 15:55
Show Gist options
  • Save johnsi15/5c1e553e9dbeba536adcc2da7fcc5e7f to your computer and use it in GitHub Desktop.
Save johnsi15/5c1e553e9dbeba536adcc2da7fcc5e7f to your computer and use it in GitHub Desktop.
Ejemplo del ámbito de las variables let y var
function varTest() {
var x = 31;
if (true) {
var x = 71; // misma variable!
console.log(x); // 71
}
console.log(x); // 71
}
function letTest() {
let x = 31;
if (true) {
let x = 71; // variable diferente
console.log(x); // 71
}
console.log(x); // 31
}
// Segundo example
var a = 5;
var b = 10;
if (a === 5) {
let a = 4; // El alcance es dentro del bloque if
var b = 1; // El alcance es dentro de la función
console.log(a); // 4
console.log(b); // 1
}
console.log(a); // 5
console.log(b); // 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment