This is one of the frequently asked questions, Even though both var and Val keywords are used to declare variables in Scala, there is a subtle difference between them. A var is a variable. It’s a mutable reference to a value. Since it’s mutable, its value might change through the program. On the other hand, the value keyword represents a value. It’s an immutable reference, which means that its value never changes. Once assigned it will always keep the same value. It’s similar to a final variable in Java or constants in other languages. It is also beneficial to remember that the variable type cannot change in Scala. It may be known as that a var behaves similarly to Java variables.
While the def is a function declaration. It is evaluated on call, similar to Python, where def is also used to declare a function.
That’s all about the difference between Var, Val, and def in Scala. In short, the value and Var are evaluated when defined, while def is evaluated on call. Also, Val defines a constant, a fixed value that cannot be modified once declared and assigned while Var defines a variable, which can be modified or reassigned.