Skip to content

Instantly share code, notes, and snippets.

@getify getify/1.js
Created Nov 13, 2012

Embed
What would you like to do?
illustrating some potential issues with adding `let` to code which already has `var` used in it...
// var: poor stylistic form, but no leakage
function foo() {
bar = 3;
if (true) {
var bar = 5;
}
}
// let: will leak an auto-global
function foo() {
bar = 3;
if (true) {
let bar = 5;
}
}
// var: common stylistic form, no leakage
function foo() {
if (true) {
var bar = 5;
}
bar = 3;
}
// let: will leak an auto-global
function foo() {
if (true) {
let bar = 5;
}
bar = 3;
}
// var: common stylistic form, no breakage
function foo() {
if (true) {
var a = 3;
var b = a + 2;
}
return b;
}
// let: will break
function foo() {
if (true) {
let a = 3;
let b = a + 2;
}
return b;
}
// var: common stylistic form, no breakage
function foo() {
if (true) {
var a = 3;
var b = a + 2;
}
return b;
}
// let: broken, certainly by accident
function foo() {
if (true) {
var a = 3;
let b = a + 2;
}
return b;
}
// let: works, probably by accident
function foo() {
if (true) {
let a = 3;
var b = a + 2;
}
return b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.