Integer Range in JavaScript


For storing the data there are various types of data types present in each programming language. Data may be in the form of string, character, number, etc. For each type of data type, there is a certain limit of memory allocated to use the memory efficiently for example, the character is a small unit and an integer may be large as compared to the character so it's better to define a character different from the integers.

To use the memory efficiently there are various data types created based on the memory they are going to take which leads to giving them a certain limit to store the characters. This certain limit gives an upper bound on how many large numbers we can store in an integer.

Different types of Numbers in JavaScript

Many programming languages divide numbers into various groups based on the size or presence of decimal digits. For example, in the C++ programming language, we have an int data type for the integers, float, and decimal data types for the non-integers real numbers while in the python programming language, there are no such types present.

In the JavaScript programming language, there is a no different types of data types present for the number and for both integers and the floating number, there is the same data type present.

Now there are two types of ranges available for the numbers in JavaScript. The first one is the largest or the maximum value that can be stored in the JavaScript variable and another is the safest value that can be stored in the JavaScript programming language.

Maximum Integer Value in JavaScript

The maximum integer value in JavaScript is the biggest value that can be stored in the JavaScript variable. That value is 1.79E+308.

There is a method present in the JavaScript that is the Number.MAX_VALUE by which we can print the above value

Let’s see the code to get the above-mentioned value by the given function −

Example

// function to get the maximum value

// that can be stored in the javascript variable 
function get_value() {
   console.log("The maximum value of the integer is: " + Number.MAX_VALUE);
}
get_value();

In the above code, we have gone through the code in JavaScript programming language to get the maximum number that can be stored in the integer by using an inbuilt method.

Minimum Positive Integer Value in JavaScript

The minimum integer value in JavaScript means the maximum number of digits we can approach after the decimal point which is the minimum floating positive number. That value is 5E-324, although this number is very low, it is still greater than zero.

There is a method present in the JavaScript that is the Number.MIN_VALUE by which we can print the above value.

Let’s see the code to get the above-mentioned value by the given function −

Example

// function to get the minimum value

// that can be stored in the javascript variable
function get_value() {
   console.log("The minimum value of the integer which is greater than zero is: " + Number.MIN_VALUE);
}
get_value(); 

In the above code, we have gone through the code in JavaScript programming language to get the number which is the minimum floating number that is greater than zero by using the inbuilt method.

We have seen two examples above which show the maximum and the minimum positive integer range that an integer can be stored in JavaScript integers. Now let's move to the case where we will see the maximum and the minimum value of an integer that it can take and it will be safe also. Because dealing with large numbers is not safe.

Maximum Positive Safe Integer Value in JavaScript

The maximum integer value in JavaScript which is safe means the maximum number of digits we can approach that is safe for computations in the JavaScript programming language. That value is 253 − 1.

There is a method present in the JavaScript that is the Number.MAX_SAFE_INTEGER by which we can print the above value.

Let’s see the code to get the above-mentioned value by the given function −

Example

// function to get the maximum safe value

// that can be stored in the javascript variable
function get_value() {
   console.log("The maximum safe value of the integer which is greater than zero is: " + Number.MAX_SAFE_INTEGER);
}
get_value();

In the above code, we have gone through the code in JavaScript programming language to get the number which is the maximum safe positive integer and its value is around 2 power 53 which is equivalent to 9E15.

Minimum Negative Safe Integer Value in JavaScript

The minimum integer value in JavaScript which is safe means the minimum number of digits we can approach that is safe for computations in the JavaScript programming language. That value is −(253 − 1).

There is a method present in the JavaScript that is the Number.MIN_SAFE_INTEGER by which we can print the above value.

Let’s see the code to get the above-mentioned value by the given function −

Example

// function to get the minimum safe value

// that can be stored in the javascript variable
function get_value() {
   console.log("The minimum safe value of the integer which is greater than zero is: " + Number.MIN_SAFE_INTEGER);
}
get_value();

In the above code, we have gone through the code in JavaScript programming language to get the number which is the maximum safe positive integer and its value is around negative 2 power 53 which is equivalent to negative of 9E15.

Conclusion

In this article we have learned about the range of integers in the JavaScript programming language. Every line in JavaScript code works for data that users want to work with and to work with the data we need to store. For storing the data there are various types of data types present in each programming language, data may be in the form of string, character, number, etc. The maximum value that can be stored in the JavaScript is 1.79E+308 and the minimum positive value that can be stored in the JavaScript is 5e-324. The maximum safe value is 2e53-1 and the minimum safe value is - (2e53-1).

Updated on: 02-Mar-2023

606 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements