val
instead of var
SC-R1061Scala allows you to declare variables using both var
and val
keywords. While var
allows you to modify or re-assign values later on, val
is strictly a constant, meaning you cannot change the value that was initially assigned. It is always recommended that you use val
if you don't intend to modify or re-assign a variable.
// `pi` is not re-assigned anywhere and can be declared as
// a `val` instead.
var pi = 3.14
val pi = 3.14