August 7, 2012

Data type conversion

JavaScript is a dynamic type language, this means that you can whenever change the type of variables by assigning a different value which is of a different type. This aspect of the language is good because it gives to the programmer the liberty to do everything he wants, but sometimes this liberty can  bring some confusion when working with variables of a specific type and treating them as another data type. This can happen when reading the value of an HTML input and treat it as a number for example, it is needed to convert the desired variable to the desired data type.
The type conversion is simple to implement for specific tasks, but if we need conversion for more universal cases or where performance is important and variables can have undesired values, what to do then?

First of all we need to analyse all data types that are present in JavaScript, there are simple data types and some complex data types. For simple data types like "boolean", "number" and "string" you can apply some universal methods of conversion from any other data type.

For boolean values is known that next values are considered false: 0, '' (empty string), null, undefined and NaN, all other values are true. So we can apply a double negation to any variable and receive a boolean value. Another way is to use the Boolean constructor.

    var a = 2;
    var b = Boolean({});
    var c;
    !!a; // true
    b; // true
    !!c; // false

The numeric values have some more methods to work with them. First of all is the Number constructor, it will convert boolean values into 0 (zero) for false and 1 for true, empty string and null -  into 0 (zero), string that contains only one numeric value with white space will be converted in respective numeric value, all other values are converted into NaN (not a number) value. For parsing strings for numeric values we can use parseInt (for integers) and parseFloat (for real numbers), these are global functions and they parse for a numeric value at the beginning of the string and return the respective numeric value. Also, JavaScript provide a way to work with variables as sequences of 32 bits using bitwise operators, this is a very fast way to convert any values into an integer.

    var a = Number(false);
    var b = parseFloat('123.4 meters');
    var c = 2.5;
    var d = -2.5;
    a; // 0
    b; // 123.4
    c | 0; // 2
    c | 1; // 3
    (d | 0) - 1; // -3
    (d | 1) - 1; // -2
    ~~(c); // 2
    ~~(d); // -2


For converting a value into a string you can use the String constructor, the concatenation with an empty string or the .toString() method, the first two can be applicable to all kind of values, the last one is applicable to all kind of values except null and undefined.

    var a = String(10);
    var b = false + '';
    var c = {};
    a; // '10'
    b; // 'false'
    c.toString(); // [object Object]

For creating objects or arrays from strings, or vice versa, you should use the global object JSON, also for arrays it is possible to use .join() and string's .split() methods to interchange their data type.

No comments:

Post a Comment