Understand Numbers

Numbers

Numbers can include decimals or no decimals. If the number is surrounded by quotes then it is evaluated as a string.

let aNumberHere = 84;
let aStringThatLooksLikeANumber = '127';

Properties & Methods

The number data type does not have too many useful properties or methods, but there is another keyword, math, that has useful properties and methods we will explore next.

Properties

Number.POSITIVE_INFINITY

Number.POSITIVE_INFINITY represents the value of positive Infinity.

console.log(Number.POSITIVE_INFINITY);

Number.NEGATIVE_INFINITY

Number.NEGATIVE_INFINITY represents the value of negative Infinity.

console.log(Number.NEGATIVE_INFINITY);

Methods

toExponential(x)

The toExponential method takes a number and expresses it in scientific format. When you pass a number as an argument it returns the number rounded to that many decimal places. Note the returned value is a string.

let num = 78979823;
console.log(num.toExponential());
// prints the string "7.8979823e+7"
console.log(num.toExponential(2));
// prints the string "7.90e+7"

toFixed(x)

The toFixed methods takes a number and the number of decimals you want as an argument. Note the returned value is a string.

console.log(1234.56789.toFixed(2));
// prints the string "1234.57"