Number in JavaScript:

The Number is a primitive data type in JavaScript. Number type represents integer, float, hexadecimal, octal or exponential value. First character in a Number type must be an integer value and it must not be enclosed in quotation marks.

Example: Number in JavaScript

var int = 100;
var float = 100.5;
var hex = 0xfff;
var exponential = 2.56e3;
var octal = 030;

Number object:

JavaScript also provides Number object which can be used with new keyword.

var hex = new Number(0xfff);

Caution: Be careful while working with String object because comparison of Number objects using == operator compares Number objects and not the values. Consider the following example.

Example: Number object comparison

var num1 = new Number(100);
var num2 = new Number(100);

var num3 = 100;

num1 == num2; // false - because num1 and num2 are two different objects
num1 == num3; // true
num1 === num3;//false

typeof(num1); // object
typeof(num3); //number

Number Properties:

The Number type includes some default properties. JavaScript treats primitive values as object, so all the properties and methods are applicable to both primitive number values and number objects.

The following table lists all the properties of Number type.

Property Description
MAX_VALUE Returns the maximum number value supported in JavaScript
MIN_VALUE Returns the smallest number value supported in JavaScript
NEGATIVE_INFINITY Returns negative infinity (-Infinity)
NaN Represents a value that is not a number.
POSITIVE_INFINITY Represents positive infinity (Infinity).
Example: Number properties

alert(' Max Value: ' + Number.MAX_VALUE +
    '\n Min Value:' + Number.MIN_VALUE +
    '\n Negative Infinity:' + Number.NEGATIVE_INFINITY +
    '\n Positive Infinity:' + Number.POSITIVE_INFINITY +
    '\n NaN:' + Number.NaN

Number Methods:

The following table lists all the methods of Number type

Method Description
toExponential(fractionDigits) Returns exponential value as a string.

var num = 100; Num.toExponential(2); // returns '1.00e+2'
toFixed(fractionDigits) Returns string of decimal value of a number based on specified fractionDigits.

var num = 100; Num.toFixed(2); // returns '100.00'
toLocaleString() Returns a number as a string value according to a browser's locale settings.

var num = 100; Num.toLocaleString(); // returns '100'
toPrecision(precisionNumber) Returns number as a string with specified total digits.

var num = 100; Num.toPrecision(4); // returns '100.0'
toString() Returns the string representation of the number value.

var num = 100; Num.toString(); // returns '100'
valueOf() Returns the value of Number object.

Example: var num = new Number(100); Num.valueOf(); // returns '100'

Points to Remember :

  1. JavaScript Number can store various numeric values like integer, float, hexadecimal, decimal, exponential and octal values.
  2. Number object can be created using new keyword. e.g. var num = new Number(100);
  3. Number type includes default properties - MAX_VALUE, MIN_VALUE, NEGATIVE_INFINITY, NaN and POSITIVE_INFINITY.
  4. Number methods are used to perform different tasks on numbers.