Number Data Type in JavaScript

The blog provides the details on the usage of the JavaScript Number Data Type. The Number Data Type in JavaScript allows to a numeric value which can be further classified as : 

  • Regular Numbers
  • BigInt Numbers

Regular Numbers in JavaScript

The regular numbers follow the IEEE-754 Standard and allows to store the values in 64-bit format. As regular numbers supports both integer or floating numbers and thus also known as “double precision floating point numbers”.

Let’s understand the possible ways for representing the Number Value

Syntax for JavaScript Number

var numValue=new Number(value);  

The below given are few more examples of assinging the number values the variables

// assign integer value
var intNumber1=5;
//assign floating point value
var floatNumber2=42.24;
// assign exponent value, returns 100
var exponentNumber3=1e2;
// assign integer value by number object
var intAssignNumber4=new Number(123);

Syntactic Sugar in JavaScript Number

The JavaScript Number can be represented in many ways and “syntactic sugar” is one among them. It means the Number value can be represented with underscore (_) but when the value is evaluated it is ignored by the JavaScript Engine and reads the value without underscore.

let numValue = 1_23_456_7890;
// The JavaScript Engine reds the value as "1234567890"

The JavaScript allows the below constant values for Number Data Type

ConstantDescription
MIN_VALUEThe constant returns largest minimum value.
MAX_VALUEThe constant returns largest maximum value.
POSITIVE_INFINITYThe constant returns positive infinity, overflow value.
NEGATIVE_INFINITYThe constant returns negative infinity, overflow value.
NaNThe constant returns “Not a Number” value.

Number MethodsDescription
isFinite()The method returns if the given value is a finite number.
isInteger()The method returns if the given value is an integer.
parseFloat()The method returns converted string into a floating point number.
parseInt()The method returns converted string into an integer number.
toExponential()The method returns if the string that represents exponential notation of the given number.
toFixed()The method returns if the string that represents a number with exact digits after a decimal point.
toPrecision()The method returns if the string representing a number of specified precision.
toString()The method returns if the given number in the form of string.

JavaScript Number isFinite() example

<script>  
var numValue=11;  
// This returns TRUE as value is finite number
document.writeln(Number.isFinite(numValue));   //returnss True
</script>

JavaScript Number isInteger() example

<script>  
var numValue1=-25;  
var numValue2=25;  
 
document.writeln(Number.isInteger(numValue1));  //returnss True
document.writeln(Number.isInteger(numValue2));  //returnss True
</script>

JavaScript Number parseFloat() example

<script>  
var numValue1=-25;  
var numValue2=25.25;  
document.writeln(Number.parseFloat(numValue1)+"<br>");  //returns -25
document.writeln(Number.parseFloat(numValue2)+"<br>");  // returns 25.25
</script>

JavaScript Number parseInt() example

<script>  
var numValue1=-25;  
var numValue2=25.25;  
document.writeln(Number.parseInt(numValue1)+"<br>");  //returns -25
document.writeln(Number.parseInt(numValue2)+"<br>");  // returns 25
</script>

JavaScript Number toExponential () example

<script>  
var numValue1=-25;  
document.writeln(numValue1.toExponential());   // returns -2.5e+1
</script> 

JavaScript Number isInteger() example

<script>  
var numValue1=25.2552; 
document.writeln(a.toPrecision());   //returns 25
</script>

JavaScript Number toString() example

<script>  
var numValue1=-25;  
var numValue2=25.25;  
document.writeln(numValue1.toString() +"<br>");  //returns -25
document.writeln(numValue2.toString() +"<br>");  //returns 2.25
</script>

BigInt Numbers in JavaScript

The below given are the features of the JavaScript bigInt Numbers

  • A bigInt is a special numeric type supporting integers of arbitrary length.
  • A bigInt variable can be created by appending n to the end of an integer literal
  • A bigInt variable can be created by calling function BigInt that creates bigInt from strings, numbers etc
  • A bigInt does not allow to combine the bigInt variables with other data type variables
  • The bigInt Numbers performs the Math Operations similar to the regular Numbers
<script> 
alert(3n +4n );  // returns 7
alert(16n / 2n); // returns 8
</script>

JavaScript BigInt Number Example

<script> 
const bigIntNumValue = 1234512345123451234554321n;    
const bigIntNumAssigValue = BigInt("5432154321543215432154321");  

document.writeln(bigIntNumValue +"<br>");     //returns 1234512345123451234554321
document.writeln(bigIntNumAssigValue +"<br>"); //returns 5432154321543215432154321

</script>