Skip to content
BENIBIX Learn

Type Conversion

Development🧩 Programming ConceptsData and Variables

Also known as Type Casting, Casting, Type Coercion

Changing a value into a new type. Text like "42" can become the number 42.

Think of it like

It is like swapping coins for a paper note. The money is the same, but the form changes.

Example

A form gives Lucy's app the age as the text "12". The app converts it to the number 12 before it adds 1.

Why it matters

Input from users and files usually arrives as text. It must be converted before it can be counted or compared.

Definition

Type conversion turns a value of one data type into another. The text "42" can become the number 42, so you can do maths with it. When you ask for it in code, it is called casting. Some languages also convert on their own, which is called coercion. That can give odd results, like "5" + 1 making "51" in JavaScript.

Example code

python
age = "12"
print(int(age) + 1)
# 13
int() turns the text "12" into the number 12, so adding works.
The first card is type conversion. The text "42" becomes the number 42.

Common mistake

Trusting that text holds a number. Converting "twelve" fails, so check the input first.

Key takeaways

  1. 1
    Same value, new type. Text 42 becomes number 42.
  2. 2
    Asked or automatic. Casting is asked; coercion is not.
  3. 3
    Can fail. Not every text is a number.

Learn it properly