In JavaScript, variables are loosely typed, so the number 5
and the string "5"
may both be treated as a number by several operators. However, is there a generic way to find out JavaScripts conversion abilites in at tunrime, or is it just the overloading of operators for several types that make the loose typing possible?
For example, given a variable a
and a string containing a type name type_canditate
, is there any way to ask JavaScript, if a
may convert to type_candidate
in a feasable manner, in contrast to the hard typing operators like instanceof
? For example, "5" instanceof Number
evaluates false, while Math.sin("5")
is perfectly feasable. For numbers, one can obviuosly check if parseFloat(some_number)
evaluates to NaN
, but this is a special case for numbers.
So, is there any generic way of naming types, and check if some variable may convert to a given type in a useful manner?