I want to be able to cast a variable to the specific type of another. As an example:
function convertToType(typevar, var) {
return (type typevar)var; // I know this doesn't work
}
so that convertToType(1, "15") returns 15, convertToType("1", 15) returns "15", convertToType(false, "True") returns true, etc.
To reiterate, I want to be able to dynamically cast variables to the types of other variables.
Is this possible?
function convertToType (t, e) {
return (t.constructor) (e);
}
note the first call when we wanted to convert 15 to a Number, we append a dot (.) to the first parameter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With