Javascript Numbers

In the previous tutorial, we have learned about Javascript data types. The number is one of them. Javascript Numbers are used in Javascript simply to perform mathematical calculations. Javascript is mainly client-side programming language but it can also be used in server-side with the node.js server.

In client-side development, numbers are used to define different kinds of shape and dimension of page interactivity. And obviously, in the back end numbers perform different kinds of calculative operation.

Numbers are declared in a variable with the following syntax in Javascript:

Variable declaration: Numbers

var myNum1 = 10, myNum2 = 7; 
document.write( "Sum is: ", myNum1 + myNum2 );
    * var is the keyword for declaring variables
    * myNum is the variable name that you will define your own
    * '=' is the assignment operator and assigns a number value to it
    * 10, 7 are values (Number values)


Sum is: 10

Note: Don’t put number values inside the single or double quotes. Otherwise, it will act as simple strings

You don’t need to define any number type or data type for declaring a variable. Javascript understands the data type itself automatically that you define.

Number Types

In Javascript, you can calculate four kinds of number types depending on the number base. They are simply:

Decimal Number Types

  • Integer: Integer numbers are most common decimal number without any fraction. ( e:g: var myInt = 5 ) Here 5 is an integer number.
  • Floating Point: Floating point numbers are a basically pure fractional number. ( e:g: var myFloat = 5.67 )

Note: You don’t need to declare an integer or floating type while declaring Javascript numbers. Because Javascript automatically detects whether it is an integer or floating point number.

Depending on base

  • Decimal Number: by default any defining any number in a variable as decimal values.
  • Binary Number: binary numbers are combinations of 0’s and 1’s. In javascript to store binary value to a variable use 0b as the initial of the binary number.
  • Octal Number: In the same way, use 0o as the initial of octal number.
  • Hexadecimal Number: You can store hexadecimal number by using 0x as the initial of hexadecimal numbers.


var a = 34; //decimal intege number
var e =  43.45; // floating point decimal number
var b = 0b110111010; // binary number
var c = 0o5715251; // octal number
var d = 0x162FA; // hexadecimal number


Don’t mess up

Numeric strings are allowed in Javascript, means if you accidentally put single or double quotation around the number value/integer value then the value type will be a string. Because Javascript handles automatically.

Note: Calculating any string with integer always executes as a string.


var x = 10, y = "30";
var z = x + y;
documnet.write( z );




Javascript Step by Step Interpretation

Javascript interpreter executes the source code statement by statement. For example, if you sum three component, like the first two components are integers and the third one is a string. In this case, Javascript interpreter will sum the first two integers in the arithmetical method.

Javascript Numbers - PrograCoding

When the execution reaches to the third string then it will be converted to a string. Let’s see an example:

var x = 5, y = 6; z = "hello';
var total = x + y + z;
console.log( total );

// expected output


Javascript Number Bit-depth

All the number you write in Javascript act as 64-bit floating point numbers. That’s why, you don’t need to declare the data type such as integer, floating point, short, long, etc.

Although for representing very large number there is another data type exists in Javascript and it is BigInt. There is some difference in Numbers and BigInts. We will learn more about BigInt in a later chapter.

Javascript Numbers Infinity

Javascript Numbers Infinity is actually a global object of Javascript properties. This property used in Javascript to determine the positive maximum value and negetive minimum value. While performing a calculation, if javascript can not handle the calculation, means the calculation result excieeds the highest maximum value or lowest minimum value then it execute +infinity or -infinity


console.log(Infinity + 1); 
console.log(Math.pow(10, 1000));


> Infinity
> Infinity
> Infinity

We will learn more about Infinity in the Javascript advanced section (Javascript Properties/Methods section).

« Previous Next »