Numbers in JavaScript

Numbers in JavaScript

To represent numeric value in javascript we need to use numbers.
As I mentioned in my previous article(Types Values and variables in Javascript) we need to use specific data types to store specific values.

In our case, if we want to store some numeric value we need to use a number data type.

JavaScript represents numbers using the 64-bit floating-point format defined by the IEEE 754 standard.

The JavaScript number format allows us to represent all numeric values between, -9,007,199,254,740,992 (-2⁵³) and 9,007,199,254,740,992 (2⁵³).
If we use values larger than this we may lose precision in trailing digits.

If a number appears directly in a JavaScript program, it called numeric literals.
JavaScript supports numeric literals in several formats.
Let’s look into it one by one.

Integer Literals

In the JavaScript program, we can use the sequence on digits from 0 to 9, to represent any base-10 numeric values.

Examples:-

5
88
56
555986547

JavaScript also allows us to use hexadecimal values(base-16). Hexadecimal literals are represented by adding 0x or 0X as a prefix to that number.
It uses 0 to 9 or a(or A) to f(or F) witch represents values from 10 to 15.

Examples:-

0xfca99       // => 1034905 = (15 × 16⁴) + (12 × 16³) + (10 × 16²) + (9 × 16¹) + (9 × 16⁰)
8873          // => 34931 = (8 × 16³) + (8 × 16²) + (7 × 16¹) + (3 × 16⁰)

In ES6 and later, we can also represent integers in binary(base-2) or octal(base-8) using prefixes 0b and 0o(or 0B and 0O) respectively.

Examples:-

0b110110110        // => (1 × 2⁸) + (1 × 2⁷) + (0 × 2⁶) + (1 × 2⁵) + (1 × 2⁴) + (0 × 2³) + (1 × 2²) + (1 × 2¹) + (0 × 2⁰)
0o57246            // => (5 × 8⁴) + (7 × 8³) + (2 × 8²) + (4 × 8¹) + (6 × 8⁰)

Floating Point literals

Floating-point literals can have decimal point.
A real value is represented as an integral part of the number, followed by a decimal point and fractional part of the number.

Floating-point literals can also be represented using exponential notation.
A real number followed by letter e(or E) with optional +/- sign, followed by an integer exponent.

This notation represents a real number multiplied by 10 to the power of the exponent.

Examples:-

3.14
55482.2287
7.9985e33     // => 7.9985 × 10²³
1.221533E-11  // => 1.221533 × 10⁻¹¹

Note:-

We can use separators in numeric literals to make them easier to read.

let no1 = 1_000_222_444;
let no2 = 2.111_125_255

At the moment, using an underscore in numeric literals is not formally standardized yet. But they are in the advanced stage of the standardization process and implemented by major browsers and Node.

Arithmetic in JavaScript

JavaScript program works with numbers using arithmetic operators, that language provides.
These includes +, -, *, /, and %.

%(Modulo) is used to get a remainder after division.

ES2016 adds ** for exponentiation.

Examples:-

20**4 // => 160000

Arithmetic in JavaScript does not raise an error in case of overflow, underflow, or division by zero.
When the number(or a result of the operation) is larger than the largest representable number(overflow), the resulting value is a special infinite value, Infinity.

Similarly, if the number(or a result of the operation) is smaller than the smallest representable value, the resulting special value is negative infinity, -Infinity.

The zero divide by zero does not have a well-defined value and the result of this operation is a special not-a-number value(NaN).

Date and Time

JavaScript defined a simple Date class for representing and manipulating numbers that represent date and time.
JavaScript Dates are objects but they also have numeric representations as timestamp, which specifies the number of elapsed milliseconds since 1st January 1970.

That’s all I wanted to cover about Numbers data type in JavaScript. In an upcoming article in this series, I will cover the Text data type of JavaScript in detail.

Hope you like it, if yes **like & share.**

Thanks for your time.

Happy coding….

← Types, values, and variables in JavaScript JavaScript Strings →