2.3.5

Type Coercion

Test yourself

Type Coercion

Type coercion allows us to operate on data of different types.

Illustrative background for Type coercionIllustrative background for Type coercion ?? "content

Type coercion

  • Usually, attempting to use data of different types in an operation (e.g. adding two numbers of different types) will cause an error.
  • Type coercion allows one of the types to be automatically cast so that the operation can be completed.
Illustrative background for Integers and floatsIllustrative background for Integers and floats ?? "content

Integers and floats

  • When working with a float and an integer, the final result will become a float.
    • E.g: 3 + 2.5 = 5.5
Illustrative background for Strings and integersIllustrative background for Strings and integers ?? "content

Strings and integers

  • When working with a string and an integer, the final result will become a string.
    • E.g: "Number = " + 3 = "Number = 3"

Jump to other topics

1Computational Thinking & Algorithms

2Programming

3Hardware & Software

4Data

5The Internet

6Online Safety & Security

Go student ad image

Unlock your full potential with GoStudent tutoring

  • Affordable 1:1 tutoring from the comfort of your home

  • Tutors are matched to your specific learning needs

  • 30+ school subjects covered

Book a free trial lesson