# JS Tip of the Day: Numeric Literal Formats

**URL:** <https://forum.kirupa.com/t/js-tip-of-the-day-numeric-literal-formats/643150>\
**Category:** web dev\
**Created:** [March 17, 2020, 1:06pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-numeric-literal-formats/643150 "2020-03-17T13:06:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 17, 2020, 1:06pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-numeric-literal-formats/643150/1 "2020-03-17T13:06:52Z")

</div>

**Numeric Literal Formats**  
Version: ES2015 (octal, binary), ES2020 (BigInt)  
Level: Beginner

There are a number of different ways to represent numbers in JavaScript source code, whether it be integer values, decimal values, or even hexadecimal. In fact, as of ES2020, there are now also two different kinds of number types in the language. Not only do you have the classic and [questionably] dependable Number type, but now, there’s the new, massive BigInt number type. Both have their own, though very similar, literal syntax. For the Number type:

```javascript
// Number literals
123 // integer
1.23 // decimal
0xDEF // hexadecimal
0o077 // octal
0b101 // binary
1.23e+5 // exponential notation

```

BigInt literals are similar to Number literals except they include an `n` suffix. BigInts also don’t support decimals or exponential syntax.

```javascript
// BigInt literals
123n // integer
0xDEFn // hexadecimal
0o077n // octal
0b101n // binary

```

Note: You may also be familiar with an older octal style in which numbers starting with just a `0`, rather than `0o`, were seen as octal values. This format is considered deprecated, and in fact not allowed in strict mode, and should be avoided.

But wait! There’s more! Looking ahead to the future of JavaScript, you may see yet more ways to represent number values, both with new types and changes to the existing literal syntax.

```javascript
// The future
1_000_000 // numeric separators (= 1000000)
1.23m // BigDecimal (decimal version of BigInt)
123@px // decorators (extended literals)

```

In fact, numeric separators are a stage 3 proposal and already available in most browsers today.

More info:

- [Numeric literals on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Numeric_literals)
- [Proposal: Numeric Separators](https://github.com/tc39/proposal-numeric-separator)
- [Proposal: BigDecimal](https://github.com/tc39/proposal-decimal)
- [Proposal: Extended Numeric Literals](https://github.com/tc39/proposal-extended-numeric-literals)

---

<div class="post-metadata">

**Author:** ![Vigus](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/vigus/32/11089_2.png) [@Vigus](https://forum.kirupa.com/u/Vigus)\
**Post date:** [March 18, 2020, 12:55pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-numeric-literal-formats/643150/2 "2020-03-18T12:55:23Z")

</div>

Why would I write a number 1000000 like this 1\_000\_000 ?  
What is the meaning of separator here?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 18, 2020, 3:47pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-numeric-literal-formats/643150/3 "2020-03-18T15:47:59Z")

</div>

They serve no functional purpose. Instead they’re useful strictly for readability. When seeing `1000000` in code, you might not realize right away that its a million. You might have to count zeros. Its worse with a billion: `1000000000`. With separators, you can more easily tell by having separators work like commas in written numbers, separating by thousand: `1_000_000_000`. That’s more easily identifiable as a billion. [The proposal](https://github.com/tc39/proposal-numeric-separator) has a few more other examples each doing something similar, separating numbers in certain ways to make them more readable.

---

<div class="post-metadata">

**Author:** ![Vigus](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/vigus/32/11089_2.png) [@Vigus](https://forum.kirupa.com/u/Vigus)\
**Post date:** [March 18, 2020, 4:38pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-numeric-literal-formats/643150/4 "2020-03-18T16:38:29Z")

</div>

True. That’s a good idea. With thousands of line of code scrolling down keeping it readable is a challenge indeed.
