How to Use math.js in NodeJS And Browser?
Last Updated :
23 Jul, 2025
Math.js can be used to perform complex mathematical computations in both NodeJS and browser environments. It is a powerful library that supports a wide range of mathematical functions, including algebra, calculus, and statistical operations.
By integrating math.js into your projects, you can handle mathematical tasks efficiently, whether you're working on server-side applications with NodeJS or client-side applications in the browser. In this article, we will learn to use Use math.js in NodeJS and Browser.
1. Using Math.js in NodeJS
Step 1: Initialize a NodeJS Project
Before you can install math.js, you need to have a NodeJS project. If you don't have one, you can create it using the following command:
npm init -y

Step 2: Install math.js
This will download and install the math.js library and adds it to your project’s dependencies in the package.json file, making it available for use in your project.
npm install mathjs
Step 3: Create a JavaScript File
Create a JavaScript file (e.g., script.js) where you will write your code to perform mathematical and statistical calculations.
Step 4: Import math.js in Your JavaScript File
We will import the math.js library into your script.js file, allowing you to access its functions and features for performing various mathematical operations.
const math = require('mathjs');In the below example code, we define an array of numbers and use math.js functions (mean, median, std) to calculate the mean, median, and standard deviation of the data, then print the results to the console.
JavaScript
// script.js
const math = require('mathjs');
const numbers = [4, 8, 15, 16, 23, 42];
const maxValue = math.max(numbers);
console.log('Maximum Value:', maxValue);
const minValue = math.min(numbers);
console.log('Minimum Value:', minValue);
const averageValue = math.mean(numbers);
console.log('Average Value:', averageValue);
const sumValue = math.sum(numbers);
console.log('Sum:', sumValue);
Step 6: Run Your JavaScript File
Execute your JavaScript file using NodeJS to see the results of your calculations.
node script.js
How to Use math.js in NodeJS2. Using Math.js in Browser
We can use math.js in the browser to perform advanced mathematical operations with ease. By including the library through a script tag, you can access its powerful functions for computations directly in your web applications.
Step 1: Include the Math.js Library
Add the math.js library to your HTML file using a <script> tag with a CDN link. This loads the library, making it available globally in your script.
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.8.0/math.min.js"></script>
Step 2: Create Your HTML File
Set up an HTML file with a basic structure, including a <script> tag where you’ll write your JavaScript code.
Step 3: Write Your JavaScript Code
Create a script.js file where you’ll use math.js functions. The below code uses math.js functions to compute the maximum and minimum values from an array.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Math.js Example</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.8.0/math.min.js"></script>
<script src="script.js"></script>
</body>
</html>
JavaScript
// script.js
const numbers = [1, 2, 3, 4, 5];
const maxValue = math.max(numbers);
console.log('Maximum Value:', maxValue);
const minValue = math.min(numbers);
console.log('Minimum Value:', minValue);
Step 4: Open Your HTML File in a Browser
Open the HTML file in a web browser to see the results of your JavaScript code in the console. You can view the console output by pressing F12 or Ctrl+Shift+I and navigating to the Console tab.
Output
How to Use math.js in Browser
Explore
Introduction & Installation
Node.js Modules , Buffer & Streams
Node.js Asynchronous Programming
Node.js NPM
Node.js Deployments & Communication
Resources & Tools