Javascript integer








To support the various types of numbers, there are several different number literal formats.

Integers can be represented as either octal (base 8) or hexadecimal (base 16) literals.

Decimal

The most basic number literal format is decimal integer, which can be entered directly as shown here:

var intNum = 105;         //integer
console.log(intNum);

Octal

For an octal literal, the first digit must be a zero (0) followed by a sequence of octal digits (numbers 0 through 7).

If a number out of this range is detected in the literal, then the leading zero is ignored and the number is treated as a decimal.


var octalNum1 = 070;     //octal for 56
console.log(octalNum1);
var octalNum2 = 079;     //invalid octal - interpreted as 79
console.log(octalNum2);
var octalNum3 = 08;      //invalid octal - interpreted as 8
console.log(octalNum3);

Octal literals are invalid when running in strict mode and will throw a syntax error.

The code above generates the following result.





Hexadecimal

To create a hexadecimal literal, you start the first two characters with 0x or 0X, followed by any number of hexadecimal digits (0 through 9, and A through F).

Letters may be in uppercase or lowercase. Here's an example:


var hexNum1 = 0xA;       //hexadecimal for 10
console.log(hexNum1);
var hexNum2 = 0x1f;      //hexedecimal for 31
console.log(hexNum2);

Numbers created using octal or hexadecimal format are treated as decimal numbers in all arithmetic operations.

The code above generates the following result.





NaN - Not A Number

NaN is short for Not a Number. In JavaScript, dividing a number by 0 returns NaN.

Any operation involving NaN always returns NaN. NaN is not equal to any value, including NaN.


console.log(NaN /10); 
console.log(NaN == 0); 
console.log(NaN == NaN); 

if ("a" != NaN){
   console.log("This is not a number");
}else{
   console.log("This is a number");
}

The code above generates the following result.

isNaN()

Javascript provides the isNaN() function, which accepts a single argument, to determine if the value is "NaN."

console.log(isNaN(NaN)); // 
console.log(isNaN(10)); //10 is a number 

The code above generates the following result.

When a value is passed into isNaN(), isNaN() tries to convert it into a number.

Some values convert into numbers directly, such as "10" or a Boolean value. For any value that cannot be converted into a number isNaN() returns true.


console.log(isNaN(NaN));       //true
console.log(isNaN(10));        //false - 10 is a number
console.log(isNaN("10"));      //false - can be converted to number 10
console.log(isNaN("asdf"));    //true - cannot be converted to a number
console.log(isNaN(true));      //false - can be converted to number 1

When isNaN() is called on objects, the object's valueOf() method is used to determine if the returned value can be converted into a number. If not, the toString() method is called and its returned value is tested as well.

The code above generates the following result.

isFinite()

To determine if a value is finite, use isFinite() function.

This function returns true only if the argument is between the minimum and the maximum values.


var result = Number.MAX_VALUE + Number.MAX_VALUE;
console.log(isFinite(result));    //false

The code above generates the following result.