This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here! Smartphone icons created by Freepik - Flaticon
4.2.2 JS Number Methods
Number methods help you work with numbers.
Number Methods and Properties
Primitive values (like 3.14 or 2014), cannot have properties and methods (because they are not objects).
But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.
The toString() Method
The toString() method returns a number as a string.
All number methods can be used on any type of numbers (literals, variables, or expressions):
var x = 123;
x.toString(); // returns 123 from variable x
(123).toString(); // returns 123 from literal 123
(100 + 23).toString(); // returns 123 from expression 100 + 23
The toExponential() Method
toExponential() returns a string, with a number rounded and written using exponential notation.
A parameter defines the number of characters behind the decimal point:
var x = 9.656;
x.toExponential(2); // returns 9.66e+0
x.toExponential(4); // returns 9.6560e+0
x.toExponential(6); // returns 9.656000e+0
The parameter is optional. If you don't specify it, JavaScript will not round the number.
The toFixed() Method
toFixed() returns a string, with the number written with a specified number of decimals:
var x = 9.656;
x.toFixed(0); // returns 10
x.toFixed(2); // returns 9.66
x.toFixed(4); // returns 9.6560
x.toFixed(6); // returns 9.656000
toFixed(2) is perfect for working with money.
The toPrecision() Method
toPrecision() returns a string, with a number written with a specified length:
var x = 9.656;
x.toPrecision(); // returns 9.656
x.toPrecision(2); // returns 9.7
x.toPrecision(4); // returns 9.656
x.toPrecision(6); // returns 9.65600
The valueOf() Method
valueOf() returns a number as a number.
var x = 123;
x.valueOf(); // returns 123 from variable x
(123).valueOf(); // returns 123 from literal 123
(100 + 23).valueOf(); // returns 123 from expression 100 + 23
In JavaScript, a number can be a primitive value (typeof = number) or an object (typeof = object).
The valueOf() method is used internally in JavaScript to convert Number objects to primitive values.
There is no reason to use it in your code .
All JavaScript data types have a valueOf() and a toString() method.
Converting Variables to Numbers
There are 3 JavaScript methods that can be used to convert variables to numbers:
The Number() method
The parseInt() method
The parseFloat() method
These methods are not number methods, but global JavaScript methods.
Global JavaScript Methods
JavaScript global methods can be used on all JavaScript data types.
These are the most relevant methods, when working with numbers:
Method
Description
Number()
Returns a number, converted from its argument.
parseFloat()
Parses its argument and returns a floating point number
parseInt()
Parses its argument and returns an integer
The Number() Method
Number() can be used to convert JavaScript variables to numbers:
Number(true); // returns 1
Number(false); // returns 0
Number("10"); // returns 10
Number(" 10"); // returns 10
Number("10 "); // returns 10
Number(" 10 "); // returns 10
Number("10.33"); // returns 10.33
Number("10,33"); // returns NaN
Number("10 33"); // returns NaN
Number("John"); // returns NaN
If the number cannot be converted, NaN (Not a Number) is returned.
The Number() Method Used on Dates
Number() can also convert a date to a number:
Number(new Date("2017-09-30")); // returns 1506729600000
The Number() method above returns the number of milliseconds since 1.1.1970.
The parseInt() Method
parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:
parseInt("10"); // returns 10
parseInt("10.33"); // returns 10
parseInt("10 20 30"); // returns 10
parseInt("10 years"); // returns 10
parseInt("years 10"); // returns NaN
If the number cannot be converted, NaN (Not a Number) is returned.
The parseFloat() Method
parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned:
parseFloat("10"); // returns 10
parseFloat("10.33"); // returns 10.33
parseFloat("10 20 30"); // returns 10
parseFloat("10 years"); // returns 10
parseFloat("years 10"); // returns NaN
If the number cannot be converted, NaN (Not a Number) is returned.
Number Properties
Property
Description
MAX_VALUE
Returns the largest number possible in JavaScript
MIN_VALUE
Returns the smallest number possible in JavaScript
POSITIVE_INFINITY
Represents infinity (returned on overflow)
NEGATIVE_INFINITY
Represents negative infinity (returned on overflow)
NaN
Represents a "Not-a-Number" value
JavaScript MIN_VALUE and MAX_VALUE
MAX_VALUE returns the largest possible number in JavaScript.
var x = Number.MAX_VALUE;
MIN_VALUE returns the lowest possible number in JavaScript.
var x = Number.MIN_VALUE;
JavaScript POSITIVE_INFINITY
var x = Number.POSITIVE_INFINITY;
POSITIVE_INFINITY is returned on overflow:
var x = 1 / 0;
JavaScript NEGATIVE_INFINITY
var x = Number.NEGATIVE_INFINITY;
NEGATIVE_INFINITY is returned on overflow:
var x = -1 / 0;
JavaScript NaN - Not a Number
var x = Number.NaN;
NaN is a JavaScript reserved word indicating that a number is not a legal number.
Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number):
var x = 100 / "Apple"; // x will be NaN (Not a Number)
Number Properties Cannot be Used on Variables
Number properties belongs to the JavaScript's number object wrapper called Number .
These properties can only be accessed as Number.MAX_VALUE .
Using myNumber .MAX_VALUE, where myNumber is a variable, expression, or value, will return undefined :
var x = 6;
var y = x.MAX_VALUE; // y becomes undefined
Complete JavaScript Number Reference
JavaScript icons used in the buttons provided by ICONS8.COM . Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Strings part 1
JS Strings part 2
JS Strings part 3
JS Strings length Property
JS Strings the escape sequence \"
JS Strings the escape sequence \'
JS Strings the escape sequence \\
JS Statements
JS Strings line break
JS Strings line break +
JS Strings line break NOT
JS Strings typeof
JS Strings - equal value
JS Strings - not equal type
JS Strings - cannot compare Objects
JS Strings - Objects cannot be compared
JS Strings slice()
JS Strings The slice() Method part 1
JS Strings The slice() Method part 2
JS Strings The slice() Method part 3
JS Strings the substring() method
JS Strings the substr() method part 1
JS Strings the substr() method part 2
JS Strings the substr() method part 3
JS Strings the replace() method part 1
JS Strings the replace() method part 2
JS Strings the replace() method part 3
Mobile test of using JS CSS
JS Strings the replace() method part 4
JS Strings the replace() method part 5
JS Strings Convert string part 1
JS Strings Convert string part 2
JS Strings Convert string part 3
JS Strings Convert string part 4
JS Strings The padStart() Method part 1
JS Strings The padStart() Method part 2
JS Strings The padStart() Method part 3
JS Strings The padEnd() Method part 1
JS Strings The padEnd() Method part 2
JS Strings The padEnd() Method part 3
JS Strings The charAt() Method
JS Strings The charCodeAt() Method
JS Strings Property Access part 1
JS Strings Property access part 2
JS String Methods String split() part 1
JS String Methods String split() part 2
JS String The indexOf() Method
JS Numbers part 1
JS Numbers part 2
JS Numbers part 3
Floating Point Precision part 1
Floating Point Precision part 2
Numbers and Strings part 1
Numbers and Strings part 2
Numbers and Strings part 3
Numbers and Strings part 4
Numbers and Strings part 5
Numbers and Strings part 6
Numbers and Strings part 7
Numbers and Strings part 8
JS Numbers, NaN (Not a Number) part 1
JS Numbers, NaN (Not a Number) part 2
JS Numbers, NaN (Not a Number) part 3
JS Numbers, NaN (Not a Number) part 4
JS Numbers, NaN (Not a Number) part 5
JS Numbers, NaN (Not a Number) part 6
JS Numbers Infinity part 1
JS Numbers Infinity part 2
JS Numbers Infinity part 3
JS Numbers - Hexadecimal part 1
JS Numbers - Hexadecimal part 2
JS Numbers As Objects part 1
JS Numbers As Objects part 2
JS Numbers As Objects part 3
JS Numbers As Objects part 4
JS Number Methods part 1
JS Number Methods part 2
JS Number Methods part 3
JS Number Methods part 4
JS Number Methods part 5
JS Number Methods part 6
JS Number Methods part 7
JS Number Methods part 8
JS Number Methods part 9
JS Number Methods MAX_VALUE Property
JS Number Methods MIN_VALUE Property
JS Number Methods POSITIVE_INFINITY Property part 1
JS Number Methods POSITIVE_INFINITY Property part 2
JS Number Methods NEGATIVE_INFINITY Property part 1
JS Number Methods NEGATIVE_INFINITY Property part 2
JS Number Methods NaN part 1
JS Number Methods NaN part 2
JS Number Methods Number Properties
JS Arrays part 1
JS Arrays part 2
JS Arrays part 3
JS Arrays part 4
JS Arrays part 5
JS Arrays part 6
JS Arrays part 7
JS Arrays part 8
JS Arrays part 9
JS Arrays part 10
JS Arrays part 11
JS Arrays part 12
JS Arrays part 13
JS Arrays part 14
JS Arrays part 15
JS Arrays part 16
JS Arrays part 17
JS Arrays part 18
JS Arrays part 19
JS Arrays part 20
JS Arrays part 21
JS Arrays isArray() Method
JS Arrays isArray() function
JS Arrays The instanceof Operator
JS Arrays The toString() Method
JS Arrays The join() Method
JS Arrays The pop() Method part 1
JS Arrays The pop() Method part 2
JS Arrays The push() Method part 1
JS Arrays The push() Method part 2
JS Arrays The shift() Method part 1
JS Arrays The shift() Method part 2
JS Arrays The shift() Method part 3
JS Arrays The shift() Method part 4
JS Arrays Bracket Indexing
JS Arrays The length Property
JS Arrays The delete Method
JS Arrays The splice() Method part 1
JS Arrays The splice() Method part 2
JS Arrays The splice() Method part 3
JS Arrays The concat() Method part 1
JS Arrays The concat() Method part 2
JS Arrays The concat() Method part 3
JS Arrays The slice() Method part 1
JS Arrays The slice() Method part 2
JS Arrays The slice() Method part 3
JS Arrays The slice() Method part 4
JS Arrays The toString() Method part 1
JS Arrays The toString() Method part 2
JS Arrays The sort() Method part 1
JS Arrays Sort in Reverse
JS Arrays The sort() Method part 2
JS Arrays The sort() Method part 3
JS Arrays The sort() Method part 4
JS Arrays The sort() Method part 5
JS Arrays The sort() Method part 6
JS Arrays The sort() Method part 7
JS Arrays The sort() Method part 8
JS Array Sort The Highest Number
JS Array Sort The lowest number
JS Arrays sort the highest number
JS Arrays sort the lowest number
JS Arrays sort car objects by age
JS Arrays sort car objects by type
JS Arrays The forEach() Method part 1
JS Arrays The forEach() Method part 2
JS Arrays The map() Method part 1
JS Arrays The map() Method part 2
JS Arrays The filter() Method part 1
JS Arrays The filter() Method part 2
JS Arrays The reduce() Method part 1
JS Arrays The reduce() Method part 2
JS Arrays The reduce() Method part 3
JS Arrays The reduceRight() Method part 1
JS Arrays The reduceRight() Method part 2