How to Declare a Boolean Variable in Swift?

Declare Boolean Variable

To declare a variable of type Bool in Swift, use : Bool after the variable name.

var isAvailable: Bool

Swift Example

Let us write a swift program, in which we declare a variable with the name isAvailable, and declare this variable as type Bool. Later, we will assign a value true to this variable and print it.

main.swift

import Foundation

var isAvailable: Bool
isAvailable = true
print(isAvailable)

Program Output

true

The type of the variable isAvailable is Bool, because of which we could assign a boolean value to the variable.

We can also programmatically check the type of the variable isAvailable using type(of:) function.

main.swift

import Foundation

var isAvailable: Bool
print(type(of: isAvailable))

Program Output

Bool

Summary

We have learnt how to declare a variable of type Bool in Swift.