Type conversion

Implicit conversion

  • To Number
    • from String (parses the string as number)
    • from Array (returns length of the array)
    • from Boolean (returns 1 if true and 0 otherwise)
    • from other types (if not default toNumber method exists, calls it, otherwise returns the number of properties not including prototype property)
  • To String
    • from Number (serializes the number to string)
    • from Array (serializes the array to string)
    • from Boolean (serializes the boolean to string)
    • from other types (if not default toString method exists, calls it, otherwise serializes the object to JSON)
  • To Array
    • from Number (creates one element array containing this object)
    • from String (creates one element array containing this object)
    • from Boolean (creates one element array containing this object)
    • from other types (if not default toArray method exists, calls it, otherwise creates one element array containing this object)
  • To Boolean
    • from Number (returns false if number is equal to 0 and true otherwise)
    • from String (returns false if string is empty and true otherwise)
    • from Array (returns false if array is empty and true otherwise)
    • from other types (if not default toBoolean method exists, calls it, otherwise returns false if the number of properties not including prototype property is equal to 0 and true otherwise)

Explicit conversion

To make make type Abc convertible to Xyz Abc.toXyz method should be defined.

There are Object.toNumber, Object.toString, Object.toArray and Object.toBoolean default conversion methods that works in the same way as implicit conversion.