← Back to Scala Course | Chapter 2: Variables & Types | Lesson 3 of 8

Boolean

A Boolean is a light switch that can only be true or false. Programs use it to answer yes-or-no questions.

In this page:

  1. Boolean
Syntax
scala
val flag: Boolean = true
val result = value1 > value2

Boolean

A Boolean holds either true or false and is the result of every comparison and logical expression. Booleans drive if statements and pattern match guards throughout Scala.

Unlike some languages, Scala never implicitly converts other types to Boolean.

Note: Comparison operators like ==, >, and < always evaluate to a Boolean value.

Example: Boolean

markup
object Main extends App {
  val isActive: Boolean = true
  val isAdmin: Boolean = false
  val isAdult = 20 >= 18

  println(s"$isActive $isAdmin $isAdult")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using = instead of == in a condition, as in if (x = 5), which does not compile.
  2. Writing True or False with a capital letter, when Scala uses true and false.
  3. Using an Int such as 1 or 0 as a condition, when Scala does not treat numbers as booleans.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.