JavaScript Number Object
These tutorials introduce the Number object. Number is the data type JavaScript uses to hold numerical values. Unlike "strongly-typed" languages, JavaScript uses a single data type to represent all kinds of numerical values, like integers of all sizes, floating points, fractions…
The JavaScript Math object is different from the Number object. The number object is used to hold numerical values in JavaScript. The Math object is used to perform operations on numerical values.
Types of Numbers in JavaScript
The single Number data type is used in JavaScript to represent any kind of numerical value. Unlike "strongly typed" programming language, JavaScript does not create a distinction between small integers, large integers, and floating points. The JavaScript interpreter considers all these simply as "Number".
Below, three types of numbers are declared:
// Declare a positive integer
var numex1 = 1;
// declare a negative integer
var numex2 = -47;
// declare a floating point number
var numex3 = 3.14;
alert( numex2.toString() +"\n"+ numex3.toString() );
The last line of our script displays the last two numbers we declared, separated by the new line character ("\n") for clarity:
We used the toString() method to convert to a string each number passed to the alert() method.
JavaScript Number Properties
We will cover the properties available to JavaScript Numbers. The properties below have a constant value, which cannot be changed by your scripts.
- The MAX_VALUE and MIN_VALUES properties respectively represent the largest and smallest numbers that can be represented by the JavaScript interpreter.
- The NaN property is a special value indicating that a value is not numerical. ("NaN" is actually an acronym for "Not a Number"...)
- The NEGATIVE_INFINITY and POSITIVE_INFINITY properties respectively symbolize a out-of-range negative or positive number (not necessarily equal to infinity).
JavaScript Number Methods
Our tutorials will cover the most common JavaScript number methods. Methods are special functions available to different types of objects; like other objects, JavaScript numbers have numerous manipulation and conversion methods.
- The toString() method returns a string representation of the number.
- The toFixed() method converts a number to a string with a specificied number of digits after the decimal point.
- The toExponential() method returns a string representation of the number in exponential notation.
- The toPrecision() method returns a string representation of the number using decimal or exponential notation (depending number size and precision requested.)
Number-related Functions
We will also cover common functions, part of the core JavaScript language, which do not actually belong to the Number object, but which are used in conjunction with Number objects.
- The isFinite() function determines whether a value is finite or not.
- The parseInt() function converts a string to an integer or returns NaN.
- The parseFloat() function converts a string to a float or returns NaN.
- The isNaN() function checks if a value is numerical or not.