Converting to particular primitive types


JavaScript has some built-in functions which can force conversion to a particular primitive type. For example:

var num = Number("23");
// typeof num === "number"

var str = String(23);
// typeof str === "string"

var bool = Boolean("foo");
// typeof bool === "boolean"

If you pass the correct primitive type to these functions, then they're a no-op, that is they just return the original value. So Number(23) will return a value of type number.

These functions can also be used as constructors, with the new operator, to create objects which represent the primitive values. This is probably inspired by things like the Integer class in Java, which is needed to ‘box’ primitive values so that they can be stored in generic collections, but I can't think of any reason to do this in JavaScript. By the way, you can get back the boxed primitive value from these objects with the .valueOf method. So, things like this work, but there's probably no point in using them:

var numobj = new Number("23");
// typeof numobj === "object"
// numobj.valueOf() === 23

var strobj = new String(23);
// typeof strobj === "object"
// strobj.valueOf() === "23"

var boolobj = new Boolean("foo");
// typeof boolobj === "object"
// boolobj.valueOf() === true

But for just forcing a conversion to a particular type, there are more terse ways of achieving it than calling these functions. For converting to a number, you can use the prefix + plus operator. (It was recently discovering this trick that inspired me to write this article.) The + operator in front of a JavaScript expression will convert it to a value of type number, just like the prefix - minus operator, but it won't change the value.

So, if the variable num contains a number already, or a string that can be converted to a number, then +num will yield a value of type number. Boolean values can also be converted to numbers in this way, with false converting to zero, and true converting to one.

Values that can't be converted to a number will end up as the special ‘NaN’ (not-a-number) value. You can test for that with the built-in isNaN function. We could use that to test if a value can be converted to a number:

function can_become_number (num) {
    return !isNaN(+num);

The simplest way of converting to a string that I can think of is doing a concatenation, since JavaScript always converts operands of the binary + plus operator to string if either one is already a string:

var str = "" + 23;
// typeof str === "string"
// str === "23"

As for booleans, any boolean operator will force conversion of any value to either true or false. So use the ! negation operator on the value twice to get a boolean representing its truthiness. The first ! operator will force the conversion, and then reverse the truthiness of the values, yielding false for anything truthy, and true for anything falsy. The second one will reverse that again, giving a boolean value representing the truthiness of the original value. For example the string "foo" is truthy:

var bool = !!"foo";
// typeof bool === "boolean"
// bool === true

Some style guides recommend using the named converter functions like Number() and it's really a matter of style whether you use those or the more compact tricks.

While I was writing this up I wrote a small test suite just to make sure I was right about it all. The tests pass in Firefox and Chrome at least.