{"id":11162,"library":"jsbi","title":"JSBI: Pure JavaScript BigInt Implementation","description":"JSBI (JavaScript BigInt) is a pure-JavaScript implementation of the ECMAScript BigInt proposal, which officially became a part of the JavaScript language in ES2020. The current stable version is 4.3.2. This library serves as a robust polyfill, enabling developers to use arbitrary-precision integers in JavaScript environments that lack native BigInt support, such as older browsers and Node.js versions. A key differentiator of JSBI is its precise adherence to the native BigInt specification's behavior, which facilitates a straightforward, mechanical migration path to native BigInts once they are universally supported, often accomplished through the `babel-plugin-transform-jsbi-to-bigint` plugin. JSBI prioritizes performance, aiming to be highly competitive with native BigInt implementations. Its release cadence is primarily driven by maintenance requirements and alignment with ECMAScript specification updates, rather than frequent feature additions, given its role as a specification-compliant polyfill.","status":"active","version":"4.3.2","language":"javascript","source_language":"en","source_url":"https://github.com/GoogleChromeLabs/jsbi","tags":["javascript","typescript"],"install":[{"cmd":"npm install jsbi","lang":"bash","label":"npm"},{"cmd":"yarn add jsbi","lang":"bash","label":"yarn"},{"cmd":"pnpm add jsbi","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Standard ESM import for consumers. The README's './jsbi.mjs' path is for internal use or specific bundler configurations, not the typical public API.","wrong":"import JSBI from './jsbi.mjs';","symbol":"JSBI","correct":"import JSBI from 'jsbi';"},{"note":"Standard CommonJS import. JSBI is exported as the default, not a named export.","wrong":"const { JSBI } = require('jsbi');","symbol":"JSBI","correct":"const JSBI = require('jsbi');"},{"note":"BigInt is a static method of the JSBI object, not a direct named export from the package.","wrong":"import { BigInt } from 'jsbi'; const myBigInt = BigInt('123');","symbol":"JSBI.BigInt","correct":"import JSBI from 'jsbi'; const myBigInt = JSBI.BigInt('123');"}],"quickstart":{"code":"import JSBI from 'jsbi';\n\nfunction demonstrateJSBI() {\n  // Install: npm install jsbi\n\n  const maxSafeInteger = Number.MAX_SAFE_INTEGER;\n  console.log(`Max safe integer (JS Number): ${maxSafeInteger}`);\n\n  // Create JSBI instances from numbers or strings\n  const max = JSBI.BigInt(maxSafeInteger);\n  console.log(`JSBI equivalent of MAX_SAFE_INTEGER: ${String(max)}`); // Use String() for clear output\n\n  const other = JSBI.BigInt('2');\n  console.log(`Another JSBI instance: ${String(other)}`);\n\n  // Perform addition using JSBI's static methods, as native operators are not overloaded\n  const result = JSBI.add(max, other);\n  console.log(`Result of JSBI.add(${String(max)}, ${String(other)}): ${String(result)}`);\n\n  // Demonstrate arithmetic beyond Number.MAX_SAFE_INTEGER\n  const largeNumber = JSBI.BigInt(\"9007199999999999\");\n  const anotherLargeNumber = JSBI.BigInt(\"12345678901234567890\");\n  const product = JSBI.multiply(largeNumber, anotherLargeNumber);\n  console.log(`Product of two large JSBI numbers: ${String(product)}`);\n\n  // Comparison operations\n  const isGreater = JSBI.greaterThan(product, JSBI.BigInt(0));\n  console.log(`Is the product greater than 0? ${isGreater}`);\n}\n\ndemonstrateJSBI();","lang":"typescript","description":"This example demonstrates how to import JSBI, create BigInt instances from numbers and strings, and perform arithmetic operations like addition and multiplication using JSBI's static methods, mimicking native BigInt behavior."},"warnings":[{"fix":"Replace native operators with corresponding `JSBI` static methods for all BigInt arithmetic. For example, `a + b` becomes `JSBI.add(a, b)`.","message":"JSBI does not polyfill native JavaScript operator overloading for BigInts (e.g., `+`, `-`, `*`, `/`). All operations must be performed using static methods provided by the `JSBI` object (e.g., `JSBI.add(a, b)`, `JSBI.multiply(a, b)`).","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Always explicitly convert `JSBI` instances to a string representation before logging, using `String(myJsbiInstance)` or `myJsbiInstance.toString()`.","message":"Directly logging a `JSBI` instance to the console (e.g., `console.log(myJsbiInstance)`) will often display the internal object representation instead of its numeric value, which can be misleading.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For new projects targeting modern environments, consider using native BigInts directly instead of JSBI. For existing projects, evaluate migration to native BigInts using `babel-plugin-transform-jsbi-to-bigint`.","message":"The primary use case for JSBI (polyfills for BigInt) is diminishing as native BigInt support is now widespread across modern browsers (Chrome 67+, Firefox 68+, Edge 79+, Safari 14+) and Node.js (v10.4+).","severity":"deprecated","affected_versions":"all"},{"fix":"Integrate `babel-plugin-transform-jsbi-to-bigint` into your Babel configuration to automatically convert JSBI syntax to native BigInts.","message":"Migrating JSBI code to native BigInt syntax requires a specific Babel plugin (`babel-plugin-transform-jsbi-to-bigint`). This adds a build-step dependency and configuration complexity.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure all operands in an arithmetic or comparison operation are `JSBI` instances and use `JSBI`'s static methods. If interconversion is necessary, use `JSBI.toNumber(jsbiValue)` or `JSBI.BigInt(numberValue)`.","cause":"Attempting to use native JavaScript operators or functions with a mix of JSBI BigInts and standard JavaScript Numbers or other primitive types.","error":"TypeError: Cannot mix BigInt and other types, use explicit conversions"},{"fix":"Call `String(myJsbiValue)` or `myJsbiValue.toString()` explicitly when logging `JSBI` instances to view their numeric value.","cause":"The default `toString` method of the `JSBI` object returns a generic object string when implicitly converted by `console.log`.","error":"console.log(myJsbiValue) outputs '[object Object]' or similar non-numeric representation."}],"ecosystem":"npm"}