Skip to content

Commit ddc0318

Browse files
committed
Updated readme: Adding section on var versus let/const
1 parent ee14d23 commit ddc0318

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,25 @@ function getFood(food) {
2121

2222
getFood(false); // undefined
2323
```
24+
25+
However, observe what happens when we replace **var** using **let**:
26+
27+
```javascript
28+
let snack = 'Meow Mix';
29+
30+
function getFood(food) {
31+
32+
if (food) {
33+
let snack = 'Friskies';
34+
return snack;
35+
}
36+
return snack;
37+
}
38+
39+
getFood(false); // 'Meow Mix'
40+
```
41+
42+
This bug highlights that we need to be careful when refactoring legacy code which uses **var**.
43+
44+
> **Best Practice**: Leave **var** declarations inside of legacy code to denote that it needs to be carefully refactored. When working on a new codebase, use **let** for variables that will change their value over time, and **const** for variables that will be immutable over time.
45+

0 commit comments

Comments
 (0)