let for values that can change, and const for values that remain constant.
Using let for Mutable Variables
Thelet keyword declares variables whose values can be reassigned:
TypeScript can infer types automatically. In the example above,
lastname is inferred as string without an explicit type annotation.Using const for Immutable Variables
Theconst keyword declares variables that cannot be reassigned:
Type Inference
TypeScript automatically infers types based on the assigned values:Best Practices
- Prefer
const: Useconstfor values that won’t change - Use
letsparingly: Only useletwhen you need to reassign the variable - Avoid
var: The oldvarkeyword has confusing scoping rules - stick withletandconst