Let vs const in JavaScript

Let or const? Which one should you use?

In JavaScript, we usually declare variables using two keywords:letwithconst.

When should we use one versus the other?

I always use by defaultconst.


becauseconstIt is guaranteed that the value cannot be reassigned.

When programming, I always think that the best thing I can use is the thing that hurts me the least.

We have many things that can cause problems.

You give something more power, you give it more responsibility.

And we usually don't want that.

Well, of course, this is worth discussing.II don't want that, it's enough for me.

If i use declare variablelet, IyieldIt is reassignable:

let number = 0
number = 1

In some cases, this is necessary.

If Imiss youVariables to be reassigned,letIs perfect.

If I don't do this (in 80% of the cases), I don't even have that option. I hope the compiler (interpreter for JS) gives me an error.

That's why I defaulted toconstEvery time I declare a variable, and only switch toletWhen I want to allow redistribution of functions.

Download mine for freeJavaScript beginner's manual

More js tutorials: