KandZ – Tuts

We like to help…!

JavaScript 9 🧬 Explicit Type Conversion

a - explicit type conversion
explicit type conversion is the process of converting a value from one data type to another
this happens by using specific methods or operators.
This differs from implicit type conversion, where JavaScript...
automatically converts types based on the context.
Understanding explicit type conversion helps you control how data types are handled in your code...
ensuring that operations are performed as expected.
It's especially useful when dealing with user input...
or interfacing with different parts of an application where data types might vary.

b - explicit conversion examples
let strNum = String(num); → to string "42"
let strBool = bool.toString(); → to string "true"
let numStr = Number(str); → to number 123
let boolNum = +"0"; → to number 0
let boolZeroStr = Boolean(zeroStr); → to boolean true
let boolNonEmptyStr = !!nonEmptyStr; → to boolean true
let objPrimitive = Object(primitive); → to object { [Number: 42] }
let intHex = parseInt(hexStr, 16); → to number from string 26 (base 16)
let floatNum = parseFloat(floatStr); → to number from string 3.14

Leave a Reply