This choice was originally made to make the language easy for beginners, so that you wouldn’t have to worry about what number type to use, but it turned out to be a very fortunate decision. Traditionally, programming languages provide a lot of control for performance reasons, so that if you know you’re using a variable to represent years you use a small number and don’t waste resources, but this is hardly a problem nowadays, as we normally have lots of RAM and CPU cycles to spare. And, of course having only one numeric type is extremely convenient.
9007199254740992+1; // 9007199254740992
There are also two ways to represent 0: -0 and +0. This is because there are two different bit representations for 0, but luckily they are treated the same. SO thread: http://stackoverflow.com/questions/7223359/are-0-and-0-the-same.
Problems with floats
With floats, the associative law does not hold.
Floating point numbers are implemented with binary fractions, but not precise arithmetics. This means that there will be some very small rounding-off that might create problems in certain situations.
0.1 + (0.2 + 0.3); // 0.6
(0.1 + 0.2) + 0.3; // 0.6000000000000001
Which leads to:
(0.1 + 0.2) + 0.3 === 0.1 + (0.2 + 0.3); // false
Although the results should obviously be the same, at the binary level they’re not, so if you compare them you’ll get
There is really no solution to this. The safest way to handle this challenge is to know about it, and use integers for operations. For instance, if you’re dealing with money you should use cents, perform your calculations, and then convert the result to $.
Another number is
NaN, which is a number that doesn’t have a value.
NaN does not equal to anything, including itself. So:
NaN === NaN // false
NaN !== NaN // true
This is kind of wrong, but that’s how it is. Basically, whenever
NaN is used in a comparison, the result will be
We can use this craziness to check if a value is
isNaN = value !== value;
There is also a more intuitive
Wrong math will return
NaN. For instance, the result of dividing 0 by 0 is
Numbers are objects + Number function
Number, which is a wrapper object to work with numeric values. If the parameter can’t be converted into a number, the result will be
var num = new Number(23); // 23 var num = new Number('nope.'); // NaN
Number literals are already object-like. You can call methods on a number literal (parenthesis are necessary to avoid ambiguity between
3.3292, for instance):
(3).toExponential(); // "3e+0" var x = 4.3832; x.toPrecision(2); // 4.4
Normally one would use number literals, but of course
Number can be used as well. One useful property is that if you omit
Number function can be used to perform type conversion:
var d = new Date('July 30, 1981'); print(Number(d)); // will print 365292000000
For a list of all properties and methods of the
Converting numbers into strings
Strings have the equivalent of the
String–which will convert what you pass to it into a string.
String can be used to convert a number into a string.
Number also has a
toString function (inherited from
Function), which will do the same thing.
The only difference between the two is that with
toString you must be sure the value you’re using is actually a number:
undefined won’t have a
toString function. On the contrary,
String(num) will always work, so I guess that would make it the preferred way to do it.
var num = 42; num.toString(); // "42 String(num) // "42"
Math should have been in
Math.floor, for instance).
Math has a lot of important properties and methods.
For a list of all properties and methods of