Big.js tutorial

last modified October 18, 2023

In this article we show to work with arbitrary precision big decimal arithmetic in JavaScript with Big.js module.


Big.js is a small, fast JavaScript library for arbitrary-precision decimal arithmetic.

In this article we work with Big.js in a Node application.

Setting up Big.js

First, we install Big.js.

$ node -v

We use Node version 18.2.0.

$ npm init -y

We initiate a new Node application.

$ npm i big.js

We install Big.js with npm i big.js command.

JavaScript Number precision error

In the first example, we show that JavaScript Numbers are not precise for doing arbitrary precision arithmetic.

var sum = 0;
// two euros fifty-five cents
var amount = 2.55;

for (let i = 0; i < 100000; i++) {

    sum += amount;


In the example, we add two euros fifty-five cents one hundred thousand times.

$ nodejs numbers.js 

We have an error in the calculation.

Big.js example

In the next example we correct the error with Big.js.

import Big from 'big.js';

let val = new Big(0.0);
let amount = new Big(2.55);

let sum = val.plus(amount).times(100000);


With Big.js library, the calculation is precise.

import Big from 'big.js';

We import Big from the big.js module.

let val = new Big(0.0);
let amount = new Big(2.55);

We create two big decimal values.

let sum = val.plus(amount).times(100000);

We add the value 100000 times. Note that the big decimal values are immutable, so we generate a new variable.

$ node main.js 

Big.js pow

The pow provides a high-precision power operation.

import Big from 'big.js';

let val = new Big(0.9);

let res = val.pow(3);

console.log(0.9 ** 3);

The example raises the 0.9 to the power of 3 using Big.js and vanilla JS.

$ node main.js


Big.js documentation

In this article we have worked with arbitrary precision arithmetic in JavaScript with the Big.js library.


My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all JavaScript tutorials.