![]() |
VOOZH | about |
In JavaScript Type Conversion can be defined as converting the data type of the variables from one type to the other manually by the programmer(explicitly) or automatically by the JavaScript(implicitly).
In JavaScript, the implicit type conversion or coercion conversion can be defined as the automatic conversion of the data type of the variables from one type to another type. Implicit type conversion mostly occurs when we are performing the arithmetic or the logical operations.
When we add a string with the number, the JavaScript automatically converts the number into a string and performs string concatenation.
When we perform the mathematical operations, then JavaScript automatically converts true to 1 and false to 0.
When we use the equality operator in the JavaScript, it compares them after converting the value into the same data type.
In JavaScript, these values are automatically converts undefined, null, "" (empty string), false, NaN, and 0 to false, and all other values to true.
In JavaScript, explicit type conversion can be defined as when we manually change the data type of the variable from one to other using some built-in JavaScript functions. JavaScript provides us the built-in functions for performing the explicit conversion.
In JavaScript, we can convert the value into a string using the String() function, toString(), and by concatenating the empty string.
In JavaScript, we can convert the value into a number using the Number() function, parseInt(), and parseFloat().
In JavaScript, we can convert the value into a boolean we can use the Boolean() function.
Conversion Type | Implicit Conversion (Coercion) | Explicit Conversion |
|---|---|---|
String + Number | Automatically converts numbers to strings | Manually convert using String() or .toString() |
Number + Boolean | Converts true to 1 and false to 0 | Use Number() to explicitly convert |
String to Boolean | Non-empty strings convert to true, empty to false | Use Boolean() for clarity |
Number from String | Implicit coercion with + operator | Use Number(), parseInt(), or parseFloat() |