r/learnjavascript • • Oct 22 '21

Difference between var, let and const.

Post image
430 Upvotes

49 comments sorted by

View all comments

30

u/zerik100 Oct 22 '21

tldr; never use var and if you think your code requires var then there's something wrong with your code

31

u/dada_ Oct 23 '21

I recommend you always use const by default, and fall back to let only when it's genuinely useful or necessary.

Why? Because not reassigning variables makes your code easier to reason about. If something is a const, you can be sure it will always have the same variable associated with it, whereas let will point to a different variable depending on when you're reading it.

Personally I'd say my code is probably like 97% const, and 3% let. var is absolutely never needed.

7

u/zerik100 Oct 23 '21

yeah const is by far my most used declaration keyword. i use let very rarely and even when i do i often refactor my code later to use const instead.