juniorScala
What are val and var in Scala?
Updated May 24, 2026
Short answer
val is immutable, var is mutable.
Deep explanation
val creates a constant reference, while var allows reassignment. Immutability is preferred for safer concurrent code.
Real-world example
Use val for configuration values in production apps.
Common mistakes
- Using var excessively leading to side effects.
Follow-up questions
- Why prefer immutability?
- Can val reference mutable objects?