All numbers, both integers and fractional decimals, are considered the type
// create a number var number = 22.347; typeof number; // 'number'
It's as simple as that! Now that we have a number, we need to learn how to perform some common tasks with numbers. Let's look at some of the properties and methods available to us.
1.79E+308. Values larger than
MAX_VALUE are represented as
var bigNumber = Number.MAX_VALUE; console.log(bigNumber); // 1.7976931348623157e+308
5e-324. Values smaller than
MIN_VALUE are converted to 0.
var smallNumber = Number.MIN_VALUE; console.log(smallNumber); // 5e-324
If we are ever in a situation when dealing with extremely large numbers we can have overflow. Overflow is simply when a number has become larger than
var bigNumber = Number.MAX_VALUE * 2; console.log(bigNumber === Number.POSITIVE_INFINITY) // true // this property can be very useful in determining whether number are too big for JS to interpret
Converting a number to a string will be an incredibly useful method. Whether we want to store our numerical data as a sting in an array or alert our user that they have "30 seconds left", we will use
var testNumber = 48; testNumber.toString(); // '48' // make a basic shot clock var number = 24; var statement = ' second shot clock'; var shotClock = number.toString() + statement; // '24 second shot clock' // there's a better way, though var shotClock = number + statement; console.log(shotClock); // '24 second shot clock' /* When a number is added to a string it will be converted to a string in the process through coercion */
This is useful if you have a number with a lot of decimal places and you want to be exact in those you see. It can also be useful for working with money. It returns the number as a string with the number of decimal places given. You will incur rounding.
var myNumber = 55.39821; myNumber.toFixed(2); // '55.40' ... and perfect for money myNumber.toFixed(4); // '55.3982' myNumber.toFixed(6); // '55.398210'
toFixed, this will return a string with a number written to a specified length. If you know you are going to display a number, but are limited to a certain number of characters, this could be useful.
var num = 391.590093; num.toPrecision(3); // '392' num.toPrecision(4); // '391.6' num.toPrecision(5); // '391.59'
var number = 55.79; console.log(Number.isInteger(number)); // false....remember integers are whole numbers
Sometimes you want to show a number in exponential format, especially with big numbers. This method returns a string, with a number rounded using exponential notation.
var num = 54483290; num.toExponential(); // '5.448329e+7' num.toExponential(2); // '5.45e+7' num.toExponential(4); // '5.4483e+7' num.toExponential(6); // '5.448329e+7' num.toExponential(8); // '5.44832900e+7'
Sometimes we need to convert other types to a number. We have some helper methods to do that.
This is used to convert other types directly to a number. If it cannot be converted to a number it returns
+ can also be used as a shorthand method.
Number('55'); // 55 Number('abc'); // NaN // + shorthand var numStr = '123'; +numStr; // 123
If you have a string and need to parse a number from it this is your friend. This method will parse a string and return a whole number (no decimal places). Spaces are allowed. Only the first number is returned. If the number cannot be converted
NaN is returned.
var num = '155.55'; parseInt(num); // 155 num = '5 10 15'; parseInt(num); // 5 ... only returns first match num = '100 dollars'; parseFloat(num); // 100 num = 'dollars 100'; parseFloat(num); // NaN
Just like the above, this method parses a string and returns a number, including decimal places. Spaces are allowed. Only the first number is returned. If the number cannot be converted
NaN is returned.
var num = '220.10'; parseFloat(num); // 220.10 num = '5.08 10.1'; parseFloat(num); // 5.08 ... only returns first match num = '100 dollars'; parseFloat(num); // 100 num = 'dollars 100'; parseFloat(num); // NaN