English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Objects

JavaScript Functions

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

JavaScript Number Methods

In the previous chapter, we have learned the basic knowledge of numbers. Let's take a step further and consider what useful operations can be performed on numbers using built-in methods and properties.

Primitive values, such as(25or3.14),cannot have properties or methods(because they are not objects).

However, in JavaScript, methods and properties can also be used with primitive values because JavaScript treats primitive values as objects when executing methods and properties.

toExponential() method

toExponential()The method returns a string representing the number as a number string.

This method takes an optional parameter that defines the number of digits after the decimal point.

var num = 12.5;
num.toExponential();  // Returns 1.25e+1
num.toExponential(2); // Returns 1.25e+1
num.toExponential(4); // Returns 1.2500e+1
num.toExponential(6); // Returns 1.250000e+1
Test See‹/›

Note:The exponential symbol can be used to represent numbers that are very large or very small in magnitude. For example,95700000000 can be written as957e8or957e + 8.

toFixed() method

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

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

var num = 12.525;
num.toFixed();  // Returns 13
num.toFixed(2); // Returns 12.53
num.toFixed(4); // Returns 12.5250
num.toFixed(6); // Returns 12.525000
Test See‹/›

If necessary, the number will be rounded, and if necessary, the decimal part will be filled with zeros to have the specified length.

toPrecision() method

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

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

var num = 5.123456;
num.toPrecision();// 5.123456
num.toPrecision(1);   // 5
num.toPrecision(2);   // 5.1
num.toPrecision(3);   // 5.12
num.toPrecision(4);   // 5.123
num.toPrecision(10);  // 5.123456000
Test See‹/›

toString() method

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

var num = 255;
num.toString();   // Returns "255as a string
Test See‹/›

ThetoString()The method can optionally accept2to36an integer parameter within a range, which specifies the base for representing the number.

var num = 255;
num.toString(10);  // Returns "255"
num.toString(16);  // Returns "ff"
num.toString(8);   // Returns "377"
num.toString(2);   // Returns "11111111"
Test See‹/›

Additionally, we can place a value inside the parentheses instead of a variable.

(9048).toString();// Returns "9048"
(false).toString();   // Returns "false"
(50 + 20).toString(); // Returns "70"
Test See‹/›

valueOf() method

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

var x = new Number(50);
typeof x.valueOf(); // Returns the number (not the object)
Test See‹/›

In JavaScript, numbers can be primitive values or objects.

ThevalueOf()method in JavaScript is used to convert the Number object to its primitive value.

a method to convert variables to numbers

has3A JavaScript Global functionCan be used to convert variables to numbers:

The Number() function

Number()The function converts the given parameters to numbers.

Returns NaN if the value cannot be converted to a number.

Number(true);   // Returns 1
Number(false);  // Returns 0
Number("100");  // Returns 100
Number("2 + 6");// Returns NaN
Number("2 6");  // Returns NaN
Number("Parrot");   // Returns NaN
Test See‹/›

isDate objectif thisNumber()The function returns the number of milliseconds since 1970 years1month1milliseconds since the day

Number(new Date());
Test See‹/›

The parseInt() function

TheparseInt()The function parses a string and returns an integer.

When you handle CSS unit values (such as15px,4em, etc.) and you need to extract the value from them, this feature is very useful.

parseInt("12");   // Returns 12
parseInt("12.25");// Returns 12
parseInt("15px"); // Returns 15
parseInt("4 days");   // Returns 4
parseInt("Day 4");// Returns NaN
parseInt("36 24 36"); // Returns 36
parseInt("   20   "); // Returns 20
Test See‹/›

Returns NaN if the first character cannot be converted to a number.

TheparseInt()The function optionally accepts an integer parameter that specifies the numeral system to be used, such as, the base16(hexadecimal) indicates that the numbers in the string should be parsed from hexadecimal to decimal.

parseInt("FF", 16);// Returns 255
parseInt("1111" 2);   // Returns 15
Test See‹/›

The parseFloat() function

parseFloat()The function parses a string and returns a floating-point number.

parseFloat("12.25");  // Returns 12.25
parseFloat("5.6em");  // Returns 5.6
parseFloat("80.5 kg");// Returns 80.5
parseFloat("weight 80.5 kg"); // Returns NaN
Test See‹/›

Returns NaN if the first character cannot be converted to a number.

JavaScript Number properties

The following table lists the properties of the Number object:

PropertyDescription
MAX_VALUEReturns the largest possible number in JavaScript
MIN_VALUEReturns the smallest possible number in JavaScript
NEGATIVE_INFINITYRepresents negative infinity (returned upon overflow)
POSITIVE_INFINITYRepresents infinity (returned upon overflow)
NaNRepresents the 'not-a-number' value

Note:All properties of Number are static, and you always use them asNumber.propertyinstead of as a property of the Number object you create.

JavaScript MIN_VALUE and MAX_VALUE

TheMAX_VALUEThe property represents the largest number that can be represented in JavaScript.

Number.MAX_VALUE;
Test See‹/›

TheMIN_VALUEThe property represents the smallest positive number that can be represented in JavaScript.

Number.MIN_VALUE;
Test See‹/›

JavaScript POSITIVE_INFINITY and NEGATIVE_INFINITY

TheNEGATIVE_INFINITYThe property represents the negative Infinity value.

Number.NEGATIVE_INFINITY;
Test See‹/›

NEGATIVE_INFINITY Overflow returns:

var num = -5 / 0;
Test See‹/›

ThePOSITIVE_INFINITYProperty represents a positive Infinity value.

Number.POSITIVE_INFINITY;
Test See‹/›

POSITIVE_INFINITY Overflow returns:

var num = 5 / 0;
Test See‹/›

JavaScript NaN-is not a number

TheNaNProperty is a representation of an illegal value.

For example, if you try to multiply a number by a string, the returned value is 'NaN'.

var ans = 10 * 'Hello';
document.getElementById('result').innerHTML = ans;
Test See‹/›

Numeric properties are static

Numeric properties are static, and you always use them asNumber.propertyinstead of as a property of the Number object you create.

For example, calling MAX_VALUE on the object you create (not the Number itself) will result in an undefined value:

var num = 20;
num.MAX_VALUE;// undefined
Test See‹/›

Complete Numeric Reference

For a complete reference to properties and methods, please visit ourJavaScript Number Reference.

The reference section includes descriptions and examples of all numeric properties and methods.