Layout Options

  • Fixed Header
    Makes the header top fixed, always visible!
  • Fixed Sidebar
    Makes the sidebar left fixed, always visible!
JavaScript Basics Number

Number Object Overview

The Number object is an object wrapper for primitive numeric values.

Creating a Number Object

To create a Number object, use the new keyword and assign the result to a variable.

The following example creates a Number object named 'myNumber':

var myNumber = new Number(value);

Note: If the value parameter cannot be converted into a number, it returns NaN (Not-a-Number).

Properties

Property Description
constructor Returns the function that created the object's prototype
MAX_VALUE Returns the largest number possible in JavaScript
MIN_VALUE Returns the smallest number possible in JavaScript
NEGATIVE_INFINITY Represents negative infinity (returned on overflow)
POSITIVE_INFINITY Represents infinity (returned on overflow)
prototype Allows addition of properties and methods to an object

Methods

Method Description
toExponential(x) Converts a number into an exponential notation
toFixed(x) Formats a number with x numbers of digits after the decimal point
toPrecision(x) Formats a number to x length
toString() Converts a Number object to a string
valueOf() Returns the primitive value of a Number object

Description

The POSITIVE_INFINITY property represents infinity, returned on overflow.

This property has the form:

Number.POSITIVE_INFINITY

Examples

The following example shows the basic use of this property.

<script>
document.write(Number.POSITIVE_INFINITY);
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Description

The prototype property allows the addition of properties and methods to an object.

This property has the form:

object.prototype.name=value

Note: Prototype is a global property which is available with almost all JavaScript objects.

Examples

The following example shows the basic use of this property.

<script>
function employee(f_name, l_name, hire_date)
{
  this.f_name = f_name;
  this.l_name = l_name;
  this.hire_date = hire_date;
}

var jsmith = new employee("John", "Smith", "06/07/2007");

employee.prototype.salary = null;
jsmith.salary = 56000;

document.write(jsmith.f_name);
document.write("<br />");
document.write(jsmith.l_name);
document.write("<br />");
document.write(jsmith.hire_date);
document.write("<br />");
document.write(jsmith.salary);
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Description

The toExponential() method is used to convert a number into an exponential notation.

This method has the form:

number.toExponential(x)
Parameter Description
x (optional) An integer between 0 and 20 representing the number of digits in the notation after the decimal point. If omitted, it is set to as many digits as necessary to represent the value

Examples

The following example shows the basic use of this method.

<script>
var num = new Number(123.45);
document.write(num.toExponential());
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Description

The toFixed() method is used to format a number to use a specified number of trailing decimals.

The number is rounded up, and nulls are added after the decimal point (if needed), to create the desired decimal length.

This method has the form:

number.toFixed(x)
Parameter Description
x (optional) The number of digits after the decimal point. Default is 0 (no digits after the decimal point)

Examples

The following example shows the basic use of this method.

<script>
var num = new Number(123.45);
document.write(num.toFixed());
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Description

The toPrecision() method is used to format a number to a specified length.

A decimal point and nulls are added (if needed), to create the specified length.

This method has the form:

number.toPrecision(x)
Parameter Description
x (optional) The number of digits. If omitted, it returns the entire number (without any formatting)

Note: This method does not change the existing strings, it only returns a copy of the joined strings.

Examples

The following example shows the basic use of this method.

<script>
var num = new Number(123.45);
document.write(num.toPrecision());
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Description

The toString() method is used to convert a Number object to a string, and return the result.

This method has the form:

number.toString(radix)
Parameter Description
radix (optional) Which base to use for representing a numeric value. Must be an integer between 2 and 36.
  • 2 - The number will show as a binary value
  • 8 - The number will show as an octal value
  • 16 - The number will show as an hexadecimal value

Note: This method is called by JavaScript automatically whenever a Number object needs to be displayed as a string.

Examples

The following example shows the basic use of this method.

<script>
var num = new Number(15);
document.write(num.toString());
document.write("<br />");
document.write(num.toString(2));
document.write("<br />");
document.write(num.toString(8));
document.write("<br />");
document.write(num.toString(16));
document.write("<br />");
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Description

The valueOf() method is used to return the primitive value of a Number object.

This method has the form:

number.valueOf()

Note: This method is usually called automatically by JavaScript, and not explicitly in code.

Examples

The following example shows the basic use of this method.

<script>
var num = new Number(15);
document.write(num.valueOf());
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Description

The constructor property returns the function that created the object's prototype.

This property has the form:

number.constructor

Examples

The following example shows the basic use of this tag.

<script>
var num = new Number();
document.write(num.constructor);
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Description

The MAX_VALUE property returns the largest number possible in JavaScript.

This static property has a value of 1.7976931348623157e+308. Numbers larger than this are represented as infinity.

This property has the form:

Number.MAX_VALUE

Examples

The following example shows the basic use of this property.

<script>
document.write(Number.MAX_VALUE);
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Description

The MIN_VALUE property returns the smallest number possible in JavaScript.

This static property has a value of 5e-324. Numbers smaller than this are converted to 0.

This property has the form:

Number.MIN_VALUE

Examples

The following example shows the basic use of this property.

<script>
document.write(Number.MIN_VALUE);
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari

Description

The NEGATIVE_INFINITY property represents negative infinity, returned on overflow.

This property has the form:

Number.NEGATIVE_INFINITY

Examples

The following example shows the basic use of this property.

<script>
document.write(Number.NEGATIVE_INFINITY);
</script>

This produces the following result:

Browser Support

Firefox IE Chrome Opera Safari