Type coercion in JavaScript
Photo: Type Coercion - ReactedNode
What you should know?
- How to declare variables and assign values to them?
- Different data types in JavaScript
- Different operators are available in JavaScript
What you will learn
- What "Type Coercion" means in JavaScript
- The difference between implicit and explicit type coercion
- The common methods used for explicit type conversion
Key concepts
What is type coercion?
Type coercion refers to JavaScript automatically or manually converting one data type to another.
There are two types of coercion:
- Implicit coercion (done automatically by JavaScript)
- Explicit coercion (done manually by the programmer)
Implicit type coercion (automatic)
JavaScript tries to guess what you mean when different types are used together.
Using the +
operator: JavaScript prioritizes string concatenation (joining strings) if either value is a string. It will convert the non-string value into a string and join them. If both are numbers, it performs mathematical addition.
Other Math Operators -
, *
, /
, %
: Math operators primarily expect numbers. If they encounter strings representing numbers, they will attempt to convert those strings to numbers before performing the calculation.
Comparison with Loose Equality ==
: The double equals operator ==
checks for equality after attempting to coerce the values to a common type.
Explicit type coercion (manual)
You can use built-in JavaScript functions to intentionally convert a value's type. This makes the intent of your code clear.
Number()
: explicitly convert a value to the Number type.String()
: explicitly convert a value to the String type.Boolean()
: explicitly convert a value to the Boolean type. This reveals whether a value is considered "truthy" or "falsy".parseInt()
: Parses a string argument and returns an integer (whole number). It reads the string from left to right and stops at the first character that isn't a digit (or ignores leading whitespace).parseFloat()
: Parses a string argument and returns a floating-point number (can include decimals). Similar to parseInt, but it recognizes the decimal point (.).
Number()
Number()
explicitly converts a value to the Number type.
String()
String()
explicitly converts a value to the String type.
parseInt()
parseInt()
parses a string argument and returns an integer (whole number). It reads the string from left to right and stops at the first character that isn't a digit (or ignores leading whitespace).
parseFloat()
parseFloat()
parses a string argument and returns a floating-point number (can include decimals). Similar to parseInt, but it recognizes the decimal point (.).
Why it matters
- Avoiding bugs: Implicit Coercion can lead to unexpected outcomes if not understood. '7' + 7 will give "77" instead of 14.
- Writing clean code: Explicit Coercion (
Number()
,String()
, etc.) is easier to read and understand. - Debugging effectively: Know why a value is not what you expect.
- Working with Input: User input (e.g., from forms) is often received as strings. You must explicitly convert this input to numbers (Number(), parseInt()) if you need to perform calculations.
- Conditional Logic: Understanding how values coerce to booleans (
Boolean()
, truthy/falsy) is fundamental for writingif
statements and other control structures that rely on conditions beingtrue
orfalse
.