Explore the difference between type conversion and type coercion in JavaScript
October 20, 2024
The definition of type conversion in ECMAScript:
The ECMAScript language implicitly performs automatic type conversion as needed. To clarify the semantics of certain constructs it is useful to define a set of conversion abstract operations. The conversion abstract operations are polymorphic; they can accept a value of any ECMAScript language type or of a Completion Record value. But no other specification types are used with these operations.
Types of Conversion
Implicit conversion: as known as type coercion: This is done automatically with the use of methods.
Explicit conversion: This is done with using built-in methods or functions.
Explicit Conversion
Below are common used methods and built-in functions.
ToBoolean
Boolean()
- !!
ToNumber
Number()
parseInt()
parseFloat()
ToString
String()
toString()
ToBoolean
ToNumber
Convert to number is a bit complicated, especially when the argument is String and Symbol.
ToString
❗️Although 0 and -0 is different when the type is number, when converting to string, both of them are "0".
Implicit Conversion (Coercion)
Type coercion typically occurs when two values of different types are used together in an operation.
Different operators will apply type conversion to operands in various ways to ensure that the operation can be performed.
Operators:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Percentage (%)
Addition
When one of the operands is a string, the other operand will be converted to a string, and string concatenation will be performed.
The conversion flow is like below:
Other Arithmetic Operators:
It forces converting a string to number.
Logic
When use loose equality, coercion is performed before comparison.
When use strict equality, type comparison comes first, if type is different, return falsy.
The flow is like below:
Logical OR and Logical AND
The operands are converted to boolean values for the logical operation.
The flow is like below:
In JavaScript, both the && (AND) and || (OR) logical operators use short-circuit evaluation. Here's a more detailed explanation:
OR (||) Operator: If the first argument evaluates to true, the overall result is true, and the second argument is not evaluated.
AND (&&) Operator: If the first argument evaluates to false, the overall result is false, and the second argument is not evaluated.
Logical NOT
The operand is converted to a boolean value and then negated.
The flow is like below:
Give a try!! Type Conversion Questions ( Created by GPT )
Explain how to explicitly convert different types to strings, numbers, and booleans, and provide example code.
Explain what implicit type conversion is and provide examples of when it occurs.
Explain the difference between == and ===, and provide examples of when to use them.
Explain why "0" == false results in true, while "0" === false results in false.
Explain what happens during arithmetic operations when numbers and strings are mixed, and provide examples.
Explain which values are converted to false and which are converted to true in conditional statements.