JavaScript Numbers

« Previous Tutorial Next Tutorial »

JavaScript number is only of one type. You can write numbers in JavaScript, with or without decimals. Here is an example of numbers in JavaScript:

var a = 23.53;
var b = 54

Hexadecimal Number in JavaScript

If a number in JavaScript are preceded by 0x, then that number is treated as hexadecimal number in JavaScript. Here is an example:

var x = 0xFF;             // x will be 255

NaN in JavaScript

NaN means Not a Number, is a JavaScript reserved word, indicates that a value is not a number. Here is an example:

var x = 100 / "Mango";  // x will be NaN (Not a Number)

Create a Number Object in JavaScript

Here is the general form to create a number object in JavaScript:

var val = new Number(number);

JavaScript Number Object Properties

Following table describes properties of the Number object in JavaScript.

Property Description
constructor holds the value of the constructor function that has created the object
MAX VALUE gives the maximum value
MIN VALUE gives the minimum value
NEGATIVE INFINITY represents the value of negative infinity
POSITIVE INFINITY represents the value of infinity
prototype adds properties and methods to the Number object

JavaScript Number Object Methods

The table given below describes methods of the Number object in JavaScript.

Method Description
toExponential(x) converts a number into an exponential notation
toFixed(x) rounds up a number to x digits after the decimal
toPrecision(x) rounds up a number to a length of x digits
toString() gives a string value for the Number object
valueOf() gives a primitive value for the Number object

JavaScript Number Example

Here is an example demonstrates Number object in JavaScript.

   <TITLE>JavaScript Number Example</TITLE>
   <SCRIPT type="text/javascript">
      js_number= new Number('15.603')
      document.write(js_number.toExponential()+ "<br>")
      document.write(js_number.toFixed()+ "<br>")


Here is the output produced by the above JavaScript Number object example program.

javascript number example

Here is the live demo output of the above Number object example in JavaScript.

JavaScript Online Test

« Previous Tutorial Next Tutorial »

© Copyright 2021. All Rights Reserved.