# JavaScript Number Methods

In previous chapter we've looked at the very basics of numbers, let's move up a gear and start thinking about what useful operations we can do on numbers with built-in methods and properties.

Primitive values, like (25 or 3.14), cannot have properties or methods (because they are not objects).

But in JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.

## The toExponential() Method

The `toExponential()` method returns a string representing the number as an exponential notation.

This method takes an optional argument that defines the number of characters behind the decimal point.

Note: Exponential notation is useful for representing numbers that are either very large or very small in magnitude. For example, 95700000000 can be written as 957e8 or 957e+8.

## The toFixed() Method

The `toFixed()` method formats a number using fixed-point notation.

The value returned by this method is a string and it has exactly specified number of digits after the decimal point.

The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

## The toPrecision() Method

The `toPrecision()` method returns a string representing the number to the specified precision.

The value returned by this method is a string and it has exactly specified number of digits after the decimal point.

## The toString() Method

The `toString()` method returns a string representing the specified number.

The `toString()` method optionally accepts an integer parameter in the range 2 through 36 specifying the base to use for representing numeric values.

Alternatively, we can put a value within parentheses rather than a variable.

## The valueOf() Method

The `valueOf()` method returns the primitive value of a number.

In JavaScript, a number can be a primitive value or an object.

The `valueOf()` method is used in JavaScript to convert Number objects to primitive values.

## Converting Variables to Numbers

There are 3 JavaScript global functions that can be used to convert variables to numbers:

• The `Number()` function
• The `parseInt()` function
• The `parseFloat()` function

## The Number() Function

The `Number()` function converts the given argument to a number.

If the value cannot be converted to a number, NaN is returned.

If the argument is a Date object, the `Number()` function returns the number of milliseconds since midnight January 1, 1970 UTC.

## The parseInt() Function

The `parseInt()` function is be used to parse an integer from a string.

This function is useful when you are dealing with the values like CSS units e.g. 15px, 4em, etc. and you want to extract the numeric value out of it.

If the first character cannot be converted to a number, NaN is returned.

The `parseInt()` function optionally accepts an integer parameter which is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

## The parseFloat() Function

The `parseFloat()` function is be used to parse floating-point number from a string.

If the first character cannot be converted to a number, NaN is returned.

## JavaScript Number Properties

The following table lists the properties of the Number object:

Property Description
`MAX_VALUE` Returns the largest number possible in JavaScript
`MIN_VALUE` Returns the smallest number possible in JavaScript
`NEGATIVE_INFINITY` Represents negative infinity (returned on overflow)
`POSITIVE_INFINITY` Represents infinity (returned on overflow)
`NaN` Represents a "Not-a-Number" value

Note: All properties of Number are static, you always use it as Number.property, rather than as a property of a Number object you created.

## JavaScript MIN_VALUE and MAX_VALUE

The `MAX_VALUE` property represents the maximum numeric value representable in JavaScript.

The `MIN_VALUE` property represents the smallest positive numeric value representable in JavaScript.

## JavaScript POSITIVE_INFINITY and NEGATIVE_INFINITY

The `NEGATIVE_INFINITY` property represents the negative Infinity value.

`NEGATIVE_INFINITY` returned on overflow:

The `POSITIVE_INFINITY` property represents the positive Infinity value.

`POSITIVE_INFINITY` returned on overflow:

## JavaScript NaN - Not a Number

The `NaN` property is a value representing Not-A-Number.

For example, if you try to multiply a number with a string the returned value will be "NaN".

## Number Properties are Static

Number properties are static, you always use it as Number.property, rather than as a property of a Number object you created.

For example calling MAX_VALUE on the object you created(rather than Number itself), will result in undefined value:

## Complete Number Reference

For a complete properties and methods reference, visit our JavaScript Number Reference.

The reference section contains descriptions and examples of all number properties and methods.