Const and Let for Variables

Is there a way to know exactly when to use the let and const for your variables. I’m aware that const can not be altered after it is assigned, but sometimes when I am making a variable I find it hard to know when using either would be appropriate. Any advice.

Here’s my personal rule for it:

  1. Use const by default when creating a new variable, this prevents variables that aren’t meant to be overriden from being unintentionally changed (and causing an error).
  2. Use let if you are initialising a variable that you know you will have to update to a new value later on the code (as using const would prevent you from doing this).

That makes sense. Thanks for your answer.

1 Like