Javascript is a great scripting language. It is simple to learn, (obviously) runs in a browser for quick development tests, and is flexible enough to allow for some powerful code to be created. That flexibility does come at a cost however.

One of the confusing aspects of Javascript is type coercion and what that means with respect to dealing with numbers. The problem is not that Javascript is flaky, it is that there are more rules to understand when dealing with a loosly-typed scripting language than a compiled language like C.

### Enough horseplay, show me examples

Let's make a simple function that takes a string and left-pads it with zeroes to a certain number of places.

function zeropad(text, numPlaces) { while(text.length < numplaces) text = "0" + text; return text; }

Here is the output with various inputs:

//Output that I expected zeropad("foo", 5); //returns "00foo" zeropad("foo", 1); //returns "foo" zeropad("123", 7); //returns 0000123 zeropad("$19.99", 7); //returns "0$19.99" //Output that I might not have expected, or that is a case that I did not plan for zeropad(123, 7); //returns 123 zeropad(234.56, 7); //returns 234.56 zeropad(false, 7); //returns false zeropad([], 7); //returns "0000000" zeropad([1], 7); //returns "0000001" zeropad([1,2,3], 7); //returns "001,2,3" zeropad({}, 7); //returns "Object {}" zeropad(new Function(), 7); //returns "0function anonymous() { }" //Exceptions thrown zeropad(null, 7); //returns TypeError: Cannot read property "length" of null zeropad(undefined, 7); //returns TypeError: Cannot read property "length" of undefined

This illustrates type coercion in action. You can see from the results that the `text`

argument can actually be any type, and the way that variable gets handled when being added to a string is different.

Strings behave as expected with zeroes simply being prepended. Numbers will actually coerce the '0' into a Number however, and result in the value of `text`

never actually changing. Other types behave more-or-less as expected with the exception perhaps being arrays. Since the coercion process will call `.toString()`

on the object, arrays will return a comma-delimited string when this happens, and an empty array will return an empty string, which will then have zeroes prepended to it.

### Rounding Numbers

There are many times when whole numbers are needed to be parsed from user input. Tax forms are a good example of this. The government does not care much about pennies on tax forms so they want everything in whole dollars. There are many ways to do this in Javascript. Given a number `x`

, we could use:

`Math.round(x)`

//round to closest whole number`Math.ceil(x)`

//round up`Math.floor(x)`

//round down`parseInt(x)`

//parse an int from any type`x.toFixed(0)`

//trim the fractional portion from a known number type`x|0`

//bitwise OR with 0 which converts x to an int first and returns x`x.toString()`

with substring math...the list goes on and on

Here is a grid showing the results of these methods applied to various types and values as inputs, as well as a few custom toInt() methods I wrote for the `akm.math`

lib:

As you can see from the grid, the results from using these different methods varies based on the data type of the input. The only (built-in) reliable method that will return an actual Number type with a useful (non-NaN) value is "bitwise OR zero" (x|0).

The `akm.math`

functions I solve the problem of both always wanting to return a useful Number, and also have somewhat intelligent parsing of values that include spaces, commas, and dollar signs. Unlike the results returned by parseInt, ex: `parseInt('123,456') === 123`

Other important notes that may not be obvious:

`typeof NaN === 'number'`

//NaN is a numeric type...just not a known number`(NaN != NaN) === true`

//one NaN may not be equal to another NaN...even itself. NaN is basically an unknown number`isNaN(parseInt(Infinity)) === true`

//parseInt(Infinity) returns NaN, not Infinity- Not all browsers render these results the same. Especially
`parseInt()`

. The default 'base' value is changing from 8 to 10

Working with numbers in Javascript can be frustrating at times. Understanding the data types that you will be working with and what options you have for conversion will help you make good decisions up front and reduce the headache from seemingly random input bugs months after you developed that webform (or game engine).