@babel/plugin-proposal-numeric-separator

Example

Decimal Literals

  1. let budget = 1_000_000_000_000;
  2. // What is the value of `budget`? It's 1 trillion!
  3. //
  4. // Let's confirm:
  5. console.log(budget === 10 ** 12); // true

Binary Literals

  1. let nibbles = 0b1010_0001_1000_0101;
  2. // Is bit 7 on? It sure is!
  3. // 0b1010_0001_1000_0101
  4. // ^
  5. //
  6. // We can double check:
  7. console.log(!!(nibbles & (1 << 7))); // true

Hex Literal

  1. // Messages are sent as 24 bit values, but should be
  2. // treated as 3 distinct bytes:
  3. let message = 0xa0_b0_c0;
  4. // What's the value of the upper most byte? It's A0, or 160.
  5. // We can confirm that:
  6. let a = (message >> 16) & 0xff;
  7. console.log(a.toString(16), a); // a0, 160
  8. // What's the value of the middle byte? It's B0, or 176.
  9. // Let's just make sure...
  10. let b = (message >> 8) & 0xff;
  11. console.log(b.toString(16), b); // b0, 176
  12. // What's the value of the lower most byte? It's C0, or 192.
  13. // Again, let's prove that:
  14. let c = message & 0xff;
  15. console.log(c.toString(16), b); // c0, 192

Octal Literal

hand wave emoji

Octals are great for permissions, but also look better when represented in 0o0000 form. No real benefit with separators here.

Installation

  1. npm install --save-dev @babel/plugin-proposal-numeric-separator

Usage

  1. {
  2. "plugins": ["@babel/plugin-proposal-numeric-separator"]
  3. }

Via CLI

  1. babel --plugins @babel/plugin-proposal-numeric-separator script.js

Via Node API

  1. require("@babel/core").transform("code", {
  2. plugins: ["@babel/plugin-proposal-numeric-separator"],
  3. });

Additional Information

If you need to further compile ES2015 Decimal, Binary, Hex and Octal number representations to their pre-ES2015 numeric literal form, add the "@babel/plugin-transform-literals" plugin:

@babel/plugin-transform-literals is already included in @babel/preset-env and @babel/preset-es2015.

  1. {
  2. "presets": ["@babel/preset-env"],
  3. "plugins": ["@babel/plugin-proposal-numeric-separator"]
  4. }
  5. {
  6. "plugins": ["@babel/plugin-proposal-numeric-separator", "@babel/plugin-transform-literals"]
  7. }

References