0

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?

dronus
  • 10,774
  • 8
  • 54
  • 80
  • There's no facility expressly for that purpose, but you can always *attempt* a conversion to a type from any other type to see what you get. – Pointy Feb 28 '14 at 15:32
  • Also, `parseFloat()` is *not* a good way to check for convertability. The `Number()` constructor is safer. – Pointy Feb 28 '14 at 15:33
  • What are the benefits of `Number` over `parseFloat`? – dronus Feb 28 '14 at 15:34
  • Try in your browser console `parseFloat("23skidoo");` – Pointy Feb 28 '14 at 15:35
  • Ok, but try `Number('')`, it evaluates to 0. So both of these conversions do not discern numbers from non-numbers. What a mess... – dronus Feb 28 '14 at 15:55
  • No, `Number("23skidoo")` returns `NaN` - don't call it with `new`, just use it as a function. – Pointy Feb 28 '14 at 15:57
  • Actually, you're right in the way that `Math.sin` for example seems to behave like `Number`. But regarding the `+` operator, we still cannot assume that `Number(a)` evaluating to `NaN` does mean we can expect a number if converted to string. Also `Number('a')==NaN` evaluates false, why is this? – dronus Feb 28 '14 at 15:59
  • @Pointy: I don't questioned `Number("23skidoo")` returning `NaN`, I just said that `Number('')` is not returning `NaN`, but `0`, while `''` is definetely not a number by human means. – dronus Feb 28 '14 at 16:02
  • Ah, yes that's true. The empty string is treated as zero by the standard conversion rules (which is pretty silly but that's the way the language is specified). – Pointy Feb 28 '14 at 17:26

1 Answers1

1

There are three primitive data types in JavaScript: string, number and boolean.

Anything can be converted to a string or boolean:

  • All objects convert to true (except null, which becomes false - I only mention it here because typeof null gives object)
  • All objects have a built-in toString method which is called when converting to a string.
  • Converting a number to a string is done by giving the string representation of the number (ie. 5 becomes "5")
  • Numbers convert to boolean true, unless it's 0 which becomes false.

Converting to a number is a little trickier, but technically possible. If it can find a valid number, then it becomes that number. Otherwise, it becomes NaN.

So basically... any type can become any other type through casting in this way. The only time you have anything resembling an "error condition" is NaN.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Objects are converted to numbers via the `valueOf()` function, which is basically the number analogue of `toString()`. – Pointy Feb 28 '14 at 15:58
  • Just asked another question about checking for numbers: http://stackoverflow.com/questions/22100243/how-to-check-if-a-string-contains-a-number-in-javascript – dronus Feb 28 '14 at 16:14