6

I just start coding in TypeScript. So please excuse my newbie question.

I have this method:

public copyToDest() { for (var i = 0; i < this.source.length; i++) { var item = this.source[i]; if (item && item.isValid) this.dest.push(item); } } 

Which is working fine. After installing a refactoring tool, I got 2 suggestions:

  1. Change var i = 0; to let i = 0;
  2. Change var item = ... to const item = ...

Is there any rule I'm missing about proper use of var, let and const? Or should I just ignore these suggestions?

1

1 Answer 1

22

Use let when the variable's value can be changed.

Use const when the variable's value cannot/should not be changed.

Do not use var.

Sign up to request clarification or add additional context in comments.

1 Comment

@Vladimir830 -- No, but var is considered conceptually flawed by many, in that its declaration is always moved to the top of the current function scope. let was introduced to offer a more sensible alternative without breaking code that already relies on the original behavior of var.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.