JavaScript Numbers Methods

Number methods can help you when working with number datatypes. The number methods are improved in ES6 (ECMAScript 6).

Keep reading to understand how the number methods and properties may help you.

JavaScript Convert Variable to Number

There are 3 important JavaScript methods that can convert a variable to a number. 

The methods are global JavaScript methods and not number methods. JavaScript global methods can be used on all JavaScript data types.

The methods are:Number() parseInt() parseFloat()

Method Description


Convert a variable to a number.


Convert a variable to an integer. (whole number without decimal point)


Convert a string variable to number with or without decimal point.

Learn more about JavaScript Numbers

The Number() Method

The Number() method can convert JavaScript variables to numbers.

NaN (Not a Number) is returned if the variable cannot be converted to a number.

JS JavaScript - The Number() Method

Number(true);             // returns 1
Number(false);            // returns 0
Number("8");              // returns 8
Number(" 8  ");           // returns 8
Number("  8");            // returns 8
Number("8  ");            // returns 8
Number("8.48");           // returns 8.48
Number("8,48");           // returns NaN
Number("8 48");           // returns NaN 
Number("Donald");         // returns NaN

The Number() Method Convert a Date

The Number() method can also convert a date to a number

JS JavaScript - The Number() Method Convert a Date

var date = new Date("2018-08-08");
// Date converted to number since 01.01.1970
alert("Date: " + date + "\n\nConverted to number: " + Number(date)); 

Learn more about JavaScript Dates

The ParseInt Method

The parseInt() method parses a string and returns a number. Spaces are allowed and only the first number is returned.

NaN (Not a Number) is returned if the number can not be converted.

JS JavaScript - The ParseInt Method

parseInt("88");         // returns 88
parseInt("88.20");      // returns 88
parseInt("88 89 90");   // returns 88
parseInt("88 days");    // returns 88
parseInt("Day 88");     // returns NaN  

The parseFloat() Method

The parseFloat() method parses a string and returns a floating point number. Spaces are allowed and only the first number is returned.

NaN (Not a Number) is returned if the number can not be converted.

JS JavaScript - The parseFloat() Method

parseFloat("88");         // returns 88
parseFloat("88.20");      // returns 88.20
parseFloat("88 89 90");   // returns 88
parseFloat("88 days");    // returns 88
parseFloat("Day 88");     // returns NaN  

Number Format Methods

JavaScript have some number format methods that are specifically used on numbers.

The methods are:
toString() toExponential() toFixed() toPrecision() valueOf()

Method Description


Return number as string.


Return string, with number rounded and written with exponential notation.


Return string, with number rounded and written using a specified number of decimals.


Return string, with a number written with a specified length.


Return number as number.

Number Format - The toString() Method

The toString()method returns a number as a string. (a sequence of characters). All JavaScript number format methods can be used with literals, variables or expressions.

JS JavaScript - Number Format - The toString() Method

var a = 88;  
a.toString();            // return 88 from variable a 
(88).toString();         // return 88 from literal 88  
(80 + 8).toString();     // return 88 from expression 80 + 8

Number Format - The toExponential() Method

The toExponential() method returns a string, with a number rounded and written using exponential notation.

A parameter defines the number of characters behind the decimal point.

JS JavaScript - Number Format - The toExponential() Method

var x = 8;  
x.toExponential(2);    // return 8.00e+0 
x.toExponential(4);    // return 8.0000e+0  
x.toExponential(6);    // return 8.000000e+0

Number Format - The toFixed() Method

The toFixed() method returns a string, with the number written with a specified number of decimals.

JS JavaScript - Number Format - The toFixed() Method

var x = 8.2468;  
x.toFixed(0);           // return 8  
x.toFixed(2);           // return 8.2 
x.toFixed(4);           // return 8.2468
x.toFixed(6);           // return 8.24680

Number Format - The toPrecision() Method

The toPrecision() method returns a string, with a number written with a specified length.

JS JavaScript - Number Format - The toPrecision() Method

var x = 8.248;  
x.toPrecision();        // return 8.248
x.toPrecision(2);       // return 8.2  
x.toPrecision(4);       // return 8.248 
x.toPrecision(6);       // return 8.24800

Number Format - The valueOf() Method

The valueOf() method returns a number as a number.

This method is used internally to turn Number objects into primitive values. Normally you don’t need to use this function.

JS JavaScript - Number Format - The valueOf() Method

var x = 84; 
x.valueOf();            // return 84 from variable x 
(84).valueOf();         // return 84 from literal 84  
(100 + 84).valueOf();   // return 184 from expression 100 + 84

Number Properties

There is a list of static properties available on the Number object.
Property Description


Return the largest number possible in JavaScript.


Return the smallest number possible in JavaScript.


Represent infinity (returned on overflow).


Represent negative infinity (returned on overflow).


Represent a "Not-a-Number" value.

Learn more about JavaScript Arithmetics Learn more about JavaScript Assignment

Number Properties Examples

JS JavaScript - Number Properties Examples

// MAX_VALUE - the largest number possible in JavaScript
alert('MAX_VALUE: ' + Number.MAX_VALUE);
// MIN_VALUE - the smallest number possible in JavaScript
alert('MAX_VALUE: ' + Number.MIN_VALUE); 
// POSITIVE_INFINITY - Return Infinity
// NEGATIVE_INFINITY - Return -Infinity 
// NaN - Return NaN
alert('NaN: ' + Number.NaN);

Number Properties On Variables

It is not possible to use the number properties on variables. Using number properties on variables will return undefined.

JS JavaScript - Number Properties On Variables

var a = 88;
var b = a.MAX_VALUE;    // b becomes undefined 


Check this out

JS JavaScript: Strings

JavaScript strings are used for storing and manipulating text.
A string can be any text inside double or single quotes.

Strings can contain letters, numbers, symbols, punctuation, and even emoji.

A JavaScript string stores a series of characters like "Jonny ....

JS JavaScript: Errors

There will be errors in the code. It is good practice to handle exceptions/errors.

Exceptions are a type of error that occurs during the execution of an application. Errors should be logged. Sometimes it is possible to recover a bug and proceed.
Other cases the user should get inform....

JS JavaScript: Comparisons

Comparison and Logical operators are used to test for true or false.

JavaScript provides three different value-comparison operations, loose equality, strict equality and They have different usage so learn when to use them.

JS JavaScript: Strict Mode

The strict context prevents some actions and throws more exceptions. Strict mode changes previously accepted "bad syntax" into errors. Strict mode will for example prevent using undeclared variables. The "use strict" directive was new in ECMAScript version 5.


JavaScript is one of the 3 core language technologies of the World Wide Web that all web developers must learn:

1.      HTML to define the content of web pages.

2.      CSS to specify the layout of web pages.

3.      JavaScript to program the behavior of web pages.

JavaScript is a scripting or programming language that allows you to implement complex things on web pages. Every time a web page does more than just sit there and display static information for you to look at, displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. − you can bet that JavaScript is probably involved.

The benefits of using JavaScript are:

1.      You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.

2.      Immediate feedback to the visitors − They don't have to wait for a page reload to see if they have forgotten to enter something.

3.      Increased interactivity − You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.

4.      Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.


Need more Honney

NDC Conferences

The NDC Conferences (Norwegian Developers Conference) are one of the world`s largest independent software conferences for .NET & Agile development. The conference covers everything you need to know within software development. The conferences are held each year in many places such as Oslo, Copenhagen, London, Sydney and Minnesota.

Update Conference Prague

Update Conference Prague is the biggest developer conference in the Czech Republic. The conference is offering sessions delivered by the top experts from all around the world. Their goal is to inspire the attendees and enrich their knowledge.

Troy Hunt

Learn about Web Security from Troy Hunt. He is a really engaged speaker that frequently give talks on conferences around the world. Troy is a Microsoft MVP and you can even learn from him on some great courses on Pluralsight.

W3Schools is a great education Web Site for learning web technologies. On W3Schools you may find well organized basic to advanced tutorials and references.

Scott Hanselman

Learn about Web Technologies and new trends from Scott Hanselman. Scott Hanselman is an author of developer books and he is an engaged speaker that usually talks about the Microsoft stack. You may meet Scott on conferences around the world.

Developer Week

DeveloperWeek is one of the world’s largest developer conferences. Developer Week focus on new dev technologies, the conference have talks and workshops for newbies and experienced audience. You can attend on a Developer Week conference different places in Nothern America.


Honneybag blog

Learn JavaScript Debugging

Searching for bugs and fixing them is called code debugging. The bugs might be difficult to diagnose and therefore it is important to have a good approach to find and fix the bugs effectively. Bugs in the code might be syntax errors or logical errors. Sometimes nothing happens when the code contains bugs and there is few indications where to search. Modern web browser includes a powerful suite of developer tools. The tools do a range of things...

Explore JavaScript Arrays

Arrays are frequently used by developers. Programming languages can have slightly different implementation of arrays. An array is an object that can store a collection of items. You may say it`s a special variable that can have more than one value. The array holds many values under a single name, and you may access the values by referring to an index number. The easiest way to create....

Ultimate Guide to HTML Colors

It is essential to give the user a good user experience. There are many things you should focus on, one of them is how you use colors on your website. We don't have to struggle with black and white TVs anymore. Users expect more and more and we can apply colors to everything we want. Texts, borders, backgrounds, shadows – in HTML they all can have whatever color you decide....

Wanna be a sheep? Check out the most popular subjects
Wanna be a little different? Check out the less popular subjects