JS: Number | JavaScript
JavaScript Number
number
is the primitive data type in JavaScript and Number
is the class that deals the numberic datum.
var x = 3;
and
var x = new Number(3);
JavaScript stores numbers, compliance with IEEE 754 standard.
A JavaScript number have 3 parts:
- Fraction or Mantissa
- Exponent
- Sign
First 52 bits 0-51 hold the value (Fraction or Mantissa), later 11 bits 52-62 hold the exponent and the last bit 63 holds the sign of the number (+/-)
Exponential Notation
JavaScript numbers can be written with their exponential:
var x = 2e50;
or
var x = 2e-50;
JavaScript number data-type is 64-bit Floating Point, Integers or Floating points decimals despite other majority of programming languages.
Parse numbers in string values
parseInt()
parseInt
function is used to convert the compatibale value in other data type to integer number. e.g.:
var s = "-15489";
var i = parseInt(s);
console.log(i);
-15489
parseInt
also used to get the integer part of a number, e.g:
var f = 6.673e-11;
var i = parseInt(f);
console.log(i);
6
parseFloat()
parseFloat
function is used to convert the compatibale value in other data type to float number. e.g.:
var s = "6.673e-11";
var f = parseFloat(s);
console.log(f);
6.673e-11
NaN
JavaScript NaN means Not a Number. NaN is a static constant in Number class i.e. Number.NaN
. JavaScript parseInt
and parseFloat
return NaN
when a string is failed to parse to Number e.g:
var n = parseInt("x");
console.log(n)
NaN
JavaScript code above reveals that parseInt
function provided a non number value that caused the return of NaN
NaN Arithmetic operations
JavaScript NaN arthimetic operations return NaN, always. e.g.:
var n = Number.NaN + 1;
console.log(n)
n = Number.NaN * 1;
console.log(n)
n = Number.NaN - 1;
console.log(n)
NaN
NaN
NaN
JavaScript NaN mathematical operations show that it always reutrn NaN
In SQL databases, aSELECT
statment, add, subtract or any other mathemetical operation withNULL
value returnsNULL
e.g.SELECT NULL + 1 FROM dual;
. In JavaScript results are same forNaN
but JavaScriptNaN
is not the counterpart of SQLNULL
.
isNaN()
JavaScript isNaN
function is used to check either a value is NaN
e.g.:
var s1 = "xyz";
var n1 = parseInt(s1);
var b1 = isNaN(n1);
console.log("s1", s1)
console.log("n1", n1)
console.log("b1", b1)
var s2 = "123";
var n2 = parseInt(s2);
var b2 = isNaN(n2);
console.log("s2", s2)
console.log("n2", n2)
console.log("b2", b2)
s1 xyz
n1 NaN
b1 true
s2 123
n2 123
b2 false
JavaScript code above reveals that when "xyz"
could not parse, isNaN
returned true
where when parsed successfully it returned false
JavaScript NaN Developers Mistakes
Sometimes JavaScript developers repeatedly make the following mistake:
var s1 = "xyz";
var n1 = parseInt(s1);
var b1 = n1 == NaN; //Equality always false;
var b2 = n1 != NaN; //Inequality always true;
var b3 = n1 === NaN; //Strict Equality always false;
var b4 = n1 !== NaN; //Strict Inequality always true;
console.log(s1);
console.log(b1);
console.log(b2);
console.log(b3);
console.log(b4);
xyz
false
true
false
true
JavaScript NaN code reveals that equality, inequality, strict equality and strict inequality return false when two NaN
operands compared. So always use isNaN
function for equality check.
NaN == NaN
orNaN === NaN
always return false when comparing two NaN operands. So always useisNaN(NaN)
instead.
Known Issues
Addition of some numeric values may give unexpected resuts. This issue may occour in both, integer and decimal numbers:
JavaScript Integer Issues
JavaScript number works fine for the expression result up to 15 digits in case of integer values.
var x= 9999999999999998;
console.log(x);
9999999999999998
var x= 9999999999999999;
console.log(x);
10000000000000000
var x = 9999999999999997;
console.log(x);
9999999999999996
JavaScript Fraction Issues
Fractions calculation is a well known JavaScript issue, e.g.:
var x= 0.0 + 0.1;
console.log(x);
var x= 0.1 + 0.1;
console.log(x);
var x= 0.2 + 0.1;//0.30000000000000004
console.log(x);
var x= 0.3 + 0.1;
console.log(x);
var x= 0.4 + 0.1;
console.log(x);
var x= 0.5 + 0.1;
console.log(x);
var x= 0.6 + 0.1;
console.log(x);
var x= 0.7 + 0.1;//0.7999999999999999
console.log(x);
var x= 0.8 + 0.1;
console.log(x);
var x= 0.9 + 0.1;
console.log(x);
var x= 1.0 + 0.1;
console.log(x);
var x= 1.1 + 0.1;//1.2000000000000002
console.log(x);
var x= 1.2 + 0.1;
console.log(x);
var x= 1.3 + 0.1;//1.4000000000000001
console.log(x);
var x= 1.4 + 0.1;
console.log(x);
var x= 1.5 + 0.1;
console.log(x);
var x= 1.6 + 0.1;//1.7000000000000002
console.log(x);
var x= 1.7 + 0.1;
console.log(x);
var x= 1.8 + 0.1;//1.9000000000000001
console.log(x);
var x= 1.9 + 0.1;
console.log(x);
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.9
1
1.1
1.2000000000000002
1.3
1.4000000000000001
1.5
1.6
1.7000000000000002
1.8
1.9000000000000001
2.0
Solution
Following is the suggested solution to the fractional numbers issues: