JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

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.

Run code

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.

Run code

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.

Run code

The toString() Method

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

Run code

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

Run code

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

Run code

The valueOf() Method

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

Run code

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 Number() function converts the given argument to a number.

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

Run code

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

Run code

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.

Run code

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.

Run code

The parseFloat() Function

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

Run code

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.

Run code

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

Run code

JavaScript POSITIVE_INFINITY and NEGATIVE_INFINITY

The NEGATIVE_INFINITY property represents the negative Infinity value.

Run code

NEGATIVE_INFINITY returned on overflow:

Run code

The POSITIVE_INFINITY property represents the positive Infinity value.

Run code

POSITIVE_INFINITY returned on overflow:

Run code

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".

Run code

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:

Run code

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.