0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

val vs var

Immutable and mutable bindings.

Two Ways to Name Values

In Scala, you bind a name to a value using either val or var.

val creates an immutable binding: once assigned, it can never point to another value. var creates a mutable binding that you can reassign later.

Choosing between them is one of the first habits a Scala backend engineer builds.

val name = "Ada"
var score = 10

Defining a val

A val is read-only. You assign it once, and the compiler guarantees it stays the same.

This makes your code easier to reason about: a val never surprises you with a changed value somewhere else.

object Main extends App {
  val pi = 3.14
  println(pi)
}

All lessons in this course

  1. val vs var
  2. Basic Types and Literals
  3. Type Inference
  4. Expressions over Statements
← Back to Scala for Backend Engineering & Functional Programming