Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does character 'n' after numeric literal mean in JavaScript?

I've seen

const num = 123456789000000000000n;

And don't know what the n at the end of numeric literal does?

At the time of writing, when searching online for "What does character 'n' after numeric literal mean in JavaScript" nothing comes up.

like image 512
Nikola Dim Avatar asked Jan 29 '20 23:01

Nikola Dim


People also ask

What does N at end of number mean?

A natural number is a number that occurs commonly and obviously in nature. As such, it is a whole, non-negative number. The set of natural numbers, denoted N, can be defined in either of two ways: N = {0, 1, 2, 3, ...}

What is N in BigInt?

A BigInt value, also sometimes just called a BigInt, is a bigint primitive, created by appending n to the end of an integer literal, or by calling the BigInt() function (without the new operator) and giving it an integer value or string value.

What are the rules given in the formation of numeric literals?

The following rules govern the formation of numeric literals: A literal must contain at least one digit. A literal must contain no more than one sign character and, if one is used, it must be the leftmost character of the string. A literal must not contain more than one decimal point.

What is numeric JavaScript?

Number is a primitive wrapper object used to represent and manipulate numbers like 37 or -9.25 . The Number constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the Number() function.


1 Answers

From MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)

A BigInt is created by appending n to the end of an integer literal — 10n — or by calling the function BigInt().

In essence, BigInt allows for storing large integers, as otherwise a large numeric literal would be converted into a floating point and lose precision of the least significant digits.

like image 136
Nikola Dim Avatar answered Oct 14 '22 19:10

Nikola Dim