JS Number Methods

JS Tutorial

JS Version

JS Objects

JS Function

JS Classes

JS Async

JS HTML DOM

JS Browser BOM

JS Web API

JS AJAX

JS JSON

JS vs JQUERY

JS Graphics

JavaScript Number Methods

Number methods enables the user to work with numbers.

Number Methods and Properties

Primitive values (like 3.14 or 2014), does not have properties and methods (because they are not objects).

But with JavaScript, methods and properties are available to primitive values, as JavaScript treats primitive values as objects while executing methods and properties.

The toString() Method

The toString() method returns a number as a string.

All number methods are used on any type of numbers (literals, variables, or expressions):

Example

<!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>

Output

JavaScript Number Methods

The toString() method converts a number to a string.

123
123
123

The toExponential() Method

toExponential() returns a string, consisting of a number rounded and written using exponential notation.

A parameter specifies the number of characters behind the decimal point:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Number Methods</h2>

<p>The toExponential() method returns a string, with the number rounded and written using exponential notation.</p>

<p>An optional parameter defines the number of digits behind the decimal point.</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>

Output

JavaScript Number Methods

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

An optional parameter defines the number of digits behind the decimal point.

9.656e+0
9.66e+0
9.6560e+0
9.656000e+0

The toFixed() Method

toFixed() returns a string, with the number written with a specified number of decimals:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Number Methods</h2>

<p>The toFixed() method rounds a number to a given number of digits.</p>

<p>For working with money, toFixed(2) is perfect.</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>

Output

JavaScript Number Methods

The toFixed() method rounds a number to a given number of digits.

For working with money, toFixed(2) is perfect.

10
9.66
9.6560
9.656000

The toPrecision() Method

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

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Number Methods</h2>

<p>The toPrecision() method returns a string, with a number written with a specified 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>

Output

JavaScript Number Methods

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

9.656
9.7
9.656
9.65600

The valueOf() Method

valueOf() returns a number as a number.

<!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>

Output

JavaScript Number Methods

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

123
123
123

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 are 3 JavaScript methods that is 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.

The Number() Method

Number() are used to convert JavaScript variables to numbers:

Example

<!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>

Output

JavaScript Global Methods

The Number() method converts variables to numbers:

The Number() Method Used on Dates

Number() can also convert a date to a number.

Example

<!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>

Output

JavaScript Global Methods

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

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

Example

<!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>

Output

JavaScript Global Methods

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

86400000

The parseInt() Method

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

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Global Functions</h2>

<h2>parseInt()</h2>

<p>The global JavaScript function parseInt() converts strings to 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>

Output

JavaScript Global Functions

parseInt()

The global JavaScript function parseInt() converts strings to numbers:

The parseFloat() Method

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

Example

<!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>

Output

JavaScript Global Methods

The parseFloat() method converts strings to numbers:

JavaScript MIN_VALUE and MAX_VALUE

MAX_VALUE returns the largest possible number in JavaScript.

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Number Properties</h2>

<p>MAX_VALUE returns the largest possible number in JavaScript.</p

<p id=”demo”></p>

<script>

let x = Number.MAX_VALUE;

document.getElementById(“demo”).innerHTML = x;

</script>

</body>

</html>

Output

JavaScript Number Properties

MAX_VALUE returns the largest possible number in JavaScript.

1.7976931348623157e+308

MIN_VALUE returns the lowest possible number in JavaScript.

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Number Properties</h2>

<p>MIN_VALUE returns the smallest number possible in JavaScript.</p>

<p id=”demo”></p>

<script>

let x = Number.MIN_VALUE;

document.getElementById(“demo”).innerHTML = x;

</script>

</body>

</html>

Output

JavaScript Number Properties

MIN_VALUE returns the smallest number possible in JavaScript.

5e-324

JavaScript POSITIVE_INFINITY

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Number Properties</h2>

<p>POSITIVE_INFINITY</p>

<p id=”demo”></p>

<script>

let x = Number.POSITIVE_INFINITY;

document.getElementById(“demo”).innerHTML = x;

</script>

</body>

</html>

Output

JavaScript Number Properties

POSITIVE_INFINITY

Infinity

JavaScript NEGATIVE_INFINITY

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Number Properties</h2>

<p>NEGATIVE_INFINITY</p>

<p id=”demo”></p

<script>

let x = Number.NEGATIVE_INFINITY;

document.getElementById(“demo”).innerHTML = x;

</script>

</body>

</html>

Output

JavaScript Number Properties

NEGATIVE_INFINITY

-Infinity


Notice: ob_end_flush(): failed to send buffer of zlib output compression (1) in /home/u212091429/domains/mydevhelp.com/public_html/wp-includes/functions.php on line 5420

Notice: ob_end_flush(): failed to send buffer of zlib output compression (1) in /home/u212091429/domains/mydevhelp.com/public_html/wp-includes/functions.php on line 5420