You can apply these specific actions to all numbers in JavaScript:
Method | Description |
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()
function changes a number into a string.
You can use number methods on any kind of numbers, whether they're written directly in your code, stored in variables, or created as part of mathematical expressions.
The toExponential() Method
The toExponential()
function gives you a string that represents a number. This string shows the number in a rounded and exponential format.
A parameter defines the number of characters behind the decimal point:
The parameter is not required. If you don't mention it, JavaScript won't round the number.
The toFixed() Method
The toFixed()
function provides a way to turn a number into a string. You can also decide how many decimal places you want in the result.
toFixed(2)
is perfect for working with money.
The toPrecision() Method
The function toPrecision()
gives you a string that represents a number with a specific length.
The valueOf() Method
valueOf()
returns a number as a number.
In JavaScript, a number can be simple (like a basic number) or it can be more complex (like an object).
The valueOf()
function is an internal tool in JavaScript. It helps change Number objects into simple values.
You don't need to include it in your code.
In JavaScript, every data type has a valueOf()
and a toString()
method.
Converting Variables to Numbers
Three JavaScript methods can change a variable into 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:
If the conversion fails, it returns NaN
(Not a Number).
The Number() Method Used on Dates
Number()
can change a date into a numerical value.
Note :
The Date()
function returns the number of milliseconds since January 1, 1970.
The number of milliseconds between 1970-01-02 and 1970-01-01 is 86400000:
The parseInt() Method
The parseInt()
function reads a text and gives back a whole number. You can have spaces in the text. It only takes the first number from the text.
If the value can't be changed into a number, it will give back 'NaN' (which means Not a Number).
The parseFloat() Method
The parseFloat()
function takes a string and gives back a number. It can handle spaces in the string. However, it only gives the first number it finds in the string.
If the number cannot be converted, NaN
(Not a Number) is returned.
Number Object Methods
These methods are associated with the Number object:
Method | Description |
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 are accessible using syntax like Number.isInteger()
.
Using X.isInteger() on a variable X will cause an error:
TypeError X.isInteger is not a function
.
The Number.isInteger() Method
The Number.isInteger()
function tells us if the given value is a whole number and returns true
if it is.
The Number.isSafeInteger() Method
A safe integer is a whole number that can be accurately shown as a double precision number.
The Number.isSafeInteger()
function checks if a number is a safe integer and returns true
if it is.
Safe integers range from -(253 - 1) to +(253 - 1).
This is safe: 9007199254740991. This is not safe: 9007199254740992.
The Number.parseFloat() Method
The Number.parseFloat()
function reads a text and gives back a numerical value.
Spaces are allowed. Only the first number is returned:
If the value can't be changed into a number, the result will be NaN
, which stands for Not a Number.
Note :
The Number methods Number.parseInt()
and Number.parseFloat()
are the same as the
Global methods parseInt()
and parseFloat()
.
The aim is to organize global elements into separate modules, allowing the JavaScript code to be used more easily outside the browser.
The Number.parseInt() Method
The function Number.parseInt()
takes a string and converts it into a whole number.
Spaces are allowed. Only the first number is returned:
If the input number cannot be changed into a valid number, the result will be NaN
, which stands for Not a Number.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Number Methods</h2>
<p>The toString() method converts a number to a string.</p>
<p id="demo"></p>
<script>
let x = 123;
document.getElementById("demo").innerHTML = x.toString() + " <br> " +(123).toString() + " <br> " +(100 + 23).toString();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Number Methods</h2>
<p>The toExponential() function gives you a string. It shows the number in a rounded form using exponential notation.</p>
<p>The optional part decides how many numbers you want after the dot.</p>
<p id="demo"></p>
<script>
let x = 9.656;
document.getElementById("demo").innerHTML = x.toExponential() + " <br> " +
x.toExponential(2) + " <br> " +
x.toExponential(4) + " <br> " +
x.toExponential(6);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Number Methods</h2>
<p>The toFixed() function helps to round a number to a specific number of decimal places.</p>
<p>For dealing with money, toFixed(2) works perfectly.</p>
<p id="demo"></p>
<script>
let x = 9.656;
document.getElementById("demo").innerHTML = x.toFixed(0) + " <br> " +
x.toFixed(2) + " <br> " +
x.toFixed(4) + " <br> " +
x.toFixed(6);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Number Methods</h2>
<p>The toPrecision() function gives back a string with a number that's written in a certain length.</p>
<p id="demo"></p>
<script>
let x = 9.656;
document.getElementById("demo").innerHTML = x.toPrecision() + " <br> " +
x.toPrecision(2) + " <br> " +
x.toPrecision(4) + " <br> " +
x.toPrecision(6);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Number Methods</h2>
<p>The valueOf() method returns a number as a number:</p>
<p id="demo"></p>
<script>
let x = 123;
document.getElementById("demo").innerHTML = x.valueOf() + " <br> " +(123).valueOf() + " <br> " +(100 + 23).valueOf();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Global Methods</h2>
<p>The Number() method converts variables to numbers:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Number(true) + " <br> " +
Number(false) + " <br> " +
Number("10") + " <br> " +
Number(" 10") + " <br> " +
Number("10 ") + " <br> " +
Number(" 10 ") + " <br> " +
Number("10.33") + " <br> " +
Number("10,33") + " <br> " +
Number("10 33") + " <br> " +
Number("John");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Global Methods</h2>
<p>The Number() method can convert a date to a number:</p>
<p id="demo"></p>
<script>
let x = new Date("1970-01-01");
document.getElementById("demo").innerHTML = Number(x);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Global Methods</h2>
<p>The Number() method can convert a date to a number:</p>
<p id="demo"></p>
<script>
let x = new Date("1970-01-02");
document.getElementById("demo").innerHTML = Number(x);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Global Methods</h2>
<p>The Number() method can convert a date to a number:</p>
<p id="demo"></p>
<script>
let x = new Date("2017-09-30");
document.getElementById("demo").innerHTML = Number(x);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Global Functions</h2>
<h2>parseInt()</h2>
<p>The global JavaScript function parseInt() changes strings into numbers.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = parseInt("-10") + " <br> " +
parseInt("-10.33") + " <br> " +
parseInt("10") + " <br> " +
parseInt("10.33") + " <br> " +
parseInt("10 6") + " <br> " +
parseInt("10 years") + " <br> " +
parseInt("years 10");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Global Methods</h2>
<p>The parseFloat() method converts strings to numbers:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = parseFloat("10") + " <br> " +
parseFloat("10.33") + " <br> " +
parseFloat("10 6") + " <br> " +
parseFloat("10 years") + " <br> " +
parseFloat("years 10");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<h2>The isInteger() Method</h2>
<p>The isInteger() method returns true if the argument is an integer.</p>
<p>Otherwise it returns false.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Number.isInteger(10) + " <br> " + Number.isInteger(10.5);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<h2>The isSafeInteger() Method</h2>
<p>The isSafeInteger() method returns true if the argument is a safe integer.</p>
<p>Otherwise it returns false.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Number.isSafeInteger(10) + " <br> " + Number.isSafeInteger(12345678901234567890);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<h2>The parseFloat() Method</h2>
<p>Number.parseFloat() converts strings to numbers:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Number.parseFloat("10") + " <br> " +
Number.parseFloat("10.33") + " <br> " +
Number.parseFloat("10 20 30") + " <br> " +
Number.parseFloat("10 years") + " <br > " +
Number.parseFloat("years 10");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<h2>The parseInt() Method</h2>
<p>Number.parseInt() converts strings to whole numbers:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Number.parseInt("-10") + " <br> " +
Number.parseInt("-10.33") + " <br> " +
Number.parseInt("10") + " <br> " +
Number.parseInt("10.33") + " <br> " +
Number.parseInt("10 6") + " <br> " +
Number.parseInt("10 years") + " <br> " +
Number.parseInt("years 10");
</script>
</body>
</html>