Type Coercion

Type coercion is when a programming language automatically converts a value from one type to another (e.g., number to string).

Types

  • Implicit: Done automatically by the language
  • Explicit: Done manually by the developer (e.g., Number("42"))

Common in Weak Typing

  • "5" + 1"51" (number coerced to string)
  • "5" - 14 (string coerced to number)

See: JavaScript Type Coercion Examples

Anki

id: type-coercion-definition deck: Computer Science::Data Types tags: data-types type-coercion

Q: What is type coercion? A: Type coercion is when a programming language converts a value from one type to another.

id: implicit-vs-explicit-coercion deck: Computer Science::Data Types tags: data-types type-coercion

Q: What is the difference between implicit and explicit type coercion? A: Implicit coercion is done automatically by the language, while explicit coercion is requested by the developer.