JavaScript Numbers

Javascript has several types for representing numbers. The most commonly used number types in javascript are:


1. **number**: the `number` type represents both integer and floating-point numbers. Javascript uses double-precision 64-bit binary format (ieee 754) to represent numbers. You can declare a number by simply assigning a numeric value to a variable, for example:

javascript
Let mynumber = 10;

The `number` type also provides several built-in methods and properties for performing operations on numbers.


2. **bigint**: the `bigint` type was introduced in ecmascript 2020 to represent arbitrarily large integers. It allows you to work with numbers beyond the safe range of the `number` type. To create a `bigint`, append the `n` suffix to the number literal or use the `bigint()` constructor, for example:

javascript
Let bignumber = 9007199254740991n;
Let anotherbigint = bigint("12345678901234567890");

The `bigint` type supports all standard arithmetic operations.


3. **nan**: the special value `nan` (not-a-number) represents an invalid or unrepresentable result of a mathematical operation. It is a property of the global `number` object. For example:

javascript
Let result = 0 / 0; // nan

You can use the `isnan()` function to check if a value is `nan`.


4. **infinity and -infinity**: the special values `infinity` and `-infinity` represent positive and negative infinity, respectively. They occur when a number exceeds the upper or lower limit of the representable range. For example:

javascript
Let positiveinfinity = infinity;
Let negativeinfinity = -infinity;

You can use the `isfinite()` function to check if a value is a finite number.



These are the main number types in javascript. Remember to use the appropriate type based on your requirements to ensure accurate calculations and prevent potential issues with precision or range limitations.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext