Skip to content
Commitwise
Menu

Type conversion

Also called: Type coercion, Casting

Turning a value of one type into another, such as an Int into a Double or the text "42" into a number.

Definition

Some conversions are written explicitly (Double(x) in Swift, x.toDouble() in Kotlin, a cast in Java), and some happen automatically (Java widens int to double; JavaScript turns values into strings or numbers when operators need them). Converting text to a number can fail, which each language reports differently: nil, null, an exception or NaN.

In practice

Int("42") is 42 in Swift, but Int("42 years") is nil, and Number("") is 0 in TypeScript.

Related terms

Esc

Type to search the whole site.