Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Which one is better to use, Var, Let or Const?

Hi everybody! I was wondering about that, which one is better to use and why? All the courses about JS is with var, but then when I check about info in sites like w3school, freecodecamp, etc, I see 'let' everywhere.

Happy new year to all!

2 Answers

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,723 Points

for variable declarations let is preferred to var as it creates block scope, eliminating troublesome hoisting errors. If you don't want the value to change, use const.

This article provides additional info:

https://dev.to/sarah_chima/var-let-and-const--whats-the-difference-69e

Hello Federico! I agree with Clayton.

Previously people used to use var, however, it is considered to be bad practice now. The var keyword causes many scope errors that annoy a lot of developers. This is why they made the let and const keywords.

let is very similar to var however, it does not cause the scope errors that var did. let can also be changed.

const is short for constant, which is a variable that you do not want to change. A variable that will have a constant value.

I hope this helps!