JavaScript: Numbers

One thing I love about JavaScript is the fact that it has one numerical data type for holding numbers and its called simply Number. Internally, Number represents a 64-bit floating point that holds all possible number values one might need. By having a single numerical data type, JavaScript avoids common programming problems such as type conversion errors, loss of accuracy, and overflow problems.

'use strict';

// JavaScript has a single type to hold all numbers
//  1. Number: is internally represented as a 64-bit floating point
//  2. No separation of an Integer type => 1 === 1.0
//  3. Reduces number type errors
//  4. Completely avoids overflow problems in short integers
function compareNumbers() {
    return 1 === 1.0 ? '1 equals 1, no doubt about it' : 'Um, that\'s not supposed to happen.';

// Crazy number operations can return strange results
//  1. NaN => is a number value that is the result of an operation that cannot produce a normal result
//  2. NaN => is not equal to any value, including itself
//  3. You can only detect NaN using the function: isNaN(number)

var nan_result = 0/0;

// Extremely large numbers
//  1. Infinity => represents all values greater than 1.79...e+308

// JavaScript has a Math object which contains methods which act on Numbers
var some_number = 12343.343;
function floor_it(some_number) {
    return Math.floor(some_number);

// Number class Constants
var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s