1. Technology

Learn Modern Unobtrusive JavaScript



One of the most common types of object that you are likely to process in your JavaScript code are numbers. In fact their use is so common that JavaScript doesn't require that you actually define them as objects.

We have of course seen examples of using numbers in the prior tutorials in this series such as in the last tutorial on loops where we used a number object to track the number of times that we had processed a for loop in order to determine which of the entries in an array object that we wanted to reference.

One time that you actually need to reference the Number object itself is when defining a new number object is where you want to use it to ensure that the content actually is a number and is not something else You can assign the value into the base Number object to have it typecast into a number. Where the content of the field being typecast is not able to be converted into a number the special value NaN (which stands for Not a Number will be returned instead. Here are some examples of defining an object that contains a number:

var numone = 3;
var numtwo = Number(myfield);
var numthree = new Number(3)
var octal = 015;
var hex = 0x2f;
var exp = 2.22e15;

With these examples the first and third statements are basically equivalent in what they do. (Actually the first creates a number primitive while the third contains a number object but in practice anywhere one can be used the other can to and so the difference doesn't impact on your coding).The fourth and fifth statements assign numbers using octal (base 8) and hexadecimal (base 16) numbers respectively (015 being the same as 13 and 0x2f being the same as 47). The final statement assigns a value in exponential (scientific) notation (2.22e22 being the same as 22200000000000000000000).

Any actual number constant can be specified anywhere in JavaScript that it is valid for a number to appear.

All of the usual arithmetic can be done on numbers using JavaScript including addition (+), subtraction (-), multiplication (*), division (/), and taking remainders (%). We can also increment (++), and decrement (--) the value contained in our number object either before or after using it in any surrounding calculation (depending on which side of the object name the ++ or -- appears). In the following examples I have named the objects being created after the value that the would be assigned to them.

var ten = 4 + 6;
var seven = 8 - 1;
var six = 3 * 2;
var eight = 16 / 2;
var three = 18 % 5;

There are three methods that can be used with numbers to determine how the number will get displayed. toFixed() which returns a string representation of the number with the specified number of decimal places, toExponential() which returns a string with the number in exponential format with the specified number of decimal places, and toPrecision() which returns a string representation of the number with the specified number of significant digits.

var a = (15.2).toFixed(3); // "15.200"
var b = (123).toExponential(2); // "1.2e1"
var c = (65432).toPrecision(3); // "65400"

Note that it is only necessary to enclose the number in parentheses when you have a number constant that you wish to use these methods on. The regular dot notation will work when a number object name is used instead.

Those properties that the Number object has are static constants that you can't change. Number.MAX_VALUE is the biggest number that can be represented by a distinct number and all bigger numbers are treated as being equal to Number.POSITIVE_INFINITY. The same applies to the negative values Number.MIN_VALUE and Number.NEGATIVE_INFINITY

The functions isNaN() and isFinite() can be used to test if a field actually is a number and if the number is within the usable number range.

The Number object takes care of all the basic arithmetic that we may want to do with numbers but there may be occasions where we want to perform more advanced mathematical operations that involve numbers. The static Math object provides properties that contain such values as pi and e and functions such as sine for when you need these more advanced operations.

More of This Tutorial

  1. About.com
  2. Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Learn Modern JavaScript
  6. Unobtrusive JavaScript - Numbers

©2014 About.com. All rights reserved.