Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

JS Tutorial

JS HOME JS Introduction JS Where To JS Output JS Statements JS Syntax JS Comments JS Variables JS Let JS Const JS Operators JS Arithmetic JS Assignment JS Data Types JS Functions JS Objects JS Object Properties JS Object Methods JS Object Display JS Object Constructors JS Events JS Strings JS String Methods JS String Search JS String Templates JS Numbers JS BigInt JS Number Methods JS Number Properties JS Arrays JS Array Methods JS Array Search JS Array Sort JS Array Iteration JS Array Const JS Dates JS Date Formats JS Date Get Methods JS Date Set Methods JS Math JS Random JS Booleans JS Comparisons JS If Else JS Switch JS Loop For JS Loop For In JS Loop For Of JS Loop While JS Break JS Iterables JS Sets JS Set Methods JS Maps JS Map Methods JS Typeof JS Type Conversion JS Destructuring JS Bitwise JS RegExp JS Precedence JS Errors JS Scope JS Hoisting JS Strict Mode JS this Keyword JS Arrow Function JS Classes JS Modules JS JSON JS Debugging JS Style Guide JS Best Practices JS Mistakes JS Performance JS Reserved Words

JS Versions

JS Versions JS 2009 (ES5) JS 2015 (ES6) JS 2016 JS 2017 JS 2018 JS 2019 JS 2020 JS 2021 JS 2022 JS 2023 JS 2024 JS IE / Edge JS History

JS Objects

Object Definitions Object Prototypes Object Methods Object Properties Object Get / Set Object Protection

JS Functions

Function Definitions Function Parameters Function Invocation Function Call Function Apply Function Bind Function Closures

JS Classes

Class Intro Class Inheritance Class Static

JS Async

JS Callbacks JS Asynchronous JS Promises JS Async/Await

JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM Forms DOM CSS DOM Animations DOM Events DOM Event Listener DOM Navigation DOM Nodes DOM Collections DOM Node Lists

JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS Web APIs

Web API Intro Web Forms API Web History API Web Storage API Web Worker API Web Fetch API Web Geolocation API

JS AJAX

AJAX Intro AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

JS JSON

JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Parse JSON Stringify JSON Objects JSON Arrays JSON Server JSON PHP JSON HTML JSON JSONP

JS vs jQuery

jQuery Selectors jQuery HTML jQuery CSS jQuery DOM

JS Graphics

JS Graphics JS Canvas JS Plotly JS Chart.js JS Google Chart JS D3.js

JS Examples

JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Interview Prep JS Bootcamp JS Certificate

JS References

JavaScript Objects HTML DOM Objects


JavaScript Number Methods


JavaScript Number Methods

These number methods can be used on all JavaScript numbers:

MethodDescription
toString()Returns a number as a string
toExponential()Returns a number written in exponential notation
toFixed()Returns a number written with a number of decimals
toPrecision()Returns a number written with a specified length
valueOf()Returns a number as a number

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):

Example

let x = 123;
x.toString();
(123).toString();
(100 + 23).toString();
Try it Yourself »

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:

Example

let x = 9.656;
x.toExponential(2);
x.toExponential(4);
x.toExponential(6);
Try it Yourself »

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:

Example

let x = 9.656;
x.toFixed(0);
x.toFixed(2);
x.toFixed(4);
x.toFixed(6);
Try it Yourself »

toFixed(2) is perfect for working with money.


The toPrecision() Method

toPrecision() returns a string, with a number written with a specified length:

Example

let x = 9.656;
x.toPrecision();
x.toPrecision(2);
x.toPrecision(4);
x.toPrecision(6);
Try it Yourself »

The valueOf() Method

valueOf() returns a number as a number.

Example

let x = 123;
x.valueOf();
(123).valueOf();
(100 + 23).valueOf();
Try it Yourself »

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 a variable to a number:

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 a whole number

The methods above are not number methods. They are global JavaScript methods.


The Number() Method

The Number() method can be used to convert JavaScript variables to numbers:

Example

Number(true);
Number(false);
Number("10");
Number("  10");
Number("10  ");
Number(" 10  ");
Number("10.33");
Number("10,33");
Number("10 33");
Number("John");
Try it Yourself »

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.

Example

Number(new Date("1970-01-01"))
Try it Yourself »

Note

The Date() method returns the number of milliseconds since 1.1.1970.

The number of milliseconds between 1970-01-02 and 1970-01-01 is 86400000:

Example

Number(new Date("1970-01-02"))
Try it Yourself »

Example

Number(new Date("2017-09-30"))
Try it Yourself »

The parseInt() Method

parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:

Example

parseInt("-10");
parseInt("-10.33");
parseInt("10");
parseInt("10.33");
parseInt("10 20 30");
parseInt("10 years");
parseInt("years 10");
Try it Yourself »

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:

Example

parseFloat("10");
parseFloat("10.33");
parseFloat("10 20 30");
parseFloat("10 years");
parseFloat("years 10");
Try it Yourself »

If the number cannot be converted, NaN (Not a Number) is returned.


Number Object Methods

These object methods belong to the Number object:

MethodDescription
Number.isInteger()Returns true if the argument is an integer
Number.isSafeInteger()Returns true if the argument is a safe integer
Number.parseFloat()Converts a string to a number
Number.parseInt()Converts a string to a whole number

Number Methods Cannot be Used on Variables

The number methods above belong to the JavaScript Number Object.

These methods can only be accessed like Number.isInteger().

Using X.isInteger() where X is a variable, will result in an error:

TypeError X.isInteger is not a function.


The Number.isInteger() Method

The Number.isInteger() method returns true if the argument is an integer.

Example

Number.isInteger(10);
Number.isInteger(10.5);
Try it Yourself »

The Number.isSafeInteger() Method

A safe integer is an integer that can be exactly represented as a double precision number.

The Number.isSafeInteger() method returns true if the argument is a safe integer.

Example

Number.isSafeInteger(10);
Number.isSafeInteger(12345678901234567890);
Try it Yourself »

Safe integers are all integers from -(253 - 1) to +(253 - 1).
This is safe: 9007199254740991. This is not safe: 9007199254740992.


The Number.parseFloat() Method

Number.parseFloat() parses a string and returns a number.

Spaces are allowed. Only the first number is returned:

Example

Number.parseFloat("10");
Number.parseFloat("10.33");
Number.parseFloat("10 20 30");
Number.parseFloat("10 years");
Number.parseFloat("years 10");
Try it Yourself »

If the number cannot be converted, NaN (Not a Number) is returned.

Note

The Number methods Number.parseInt() and Number.parseFloat()

are the same as the

Global methods parseInt() and parseFloat().

The purpose is modularization of globals (to make it easier to use the same JavaScript code outside the browser).


The Number.parseInt() Method

Number.parseInt() parses a string and returns a whole number.

Spaces are allowed. Only the first number is returned:

Example

Number.parseInt("-10");
Number.parseInt("-10.33");
Number.parseInt("10");
Number.parseInt("10.33");
Number.parseInt("10 20 30");
Number.parseInt("10 years");
Number.parseInt("years 10");
Try it Yourself »

If the number cannot be converted, NaN (Not a Number) is returned.

Complete JavaScript Number Reference

For a complete Number reference, visit our:

Complete JavaScript Number Reference.

The reference contains descriptions and examples of all Number properties and methods.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.