Math.js can be used to perform basic arithmetic operations like addition, subtraction, multiplication, and division. It provides a complete library of functions and methods that handle these operations efficiently.
By using functions such as math.add, math.subtract, math.multiply, and math.divide, users can quickly compute and manipulate numerical values in their JavaScript applications. In this article, we will learn to Perform Basic Arithmetic Operations with math.js
Steps to Perform Basic Arithmetic OperationStep 1: Initialize a Node.js ProjectBefore you can install math.js , you need to have a Node.js project. If you don’t have one, you can create it using the following command:
npm init -y  Output Step 2: Install math.js Run the below command before running the code in your local system. 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 i mathjs Basic Arithmetic Operations with math.js FunctionsIn this approach, we are using the math.js library to perform basic arithmetic operations. We define two numbers, a and b, and then use math.add, math.subtract, math.multiply, and math.divide functions to compute their sum, difference, product, and quotient, respectively. Finally, we log the results to the console for each operation.
Example: The below example performs Basic Arithmetic Operations with math.js Functions.
JavaScript
// script.js
const math = require("mathjs");
const a = 10;
const b = 5;
const addition = math.add(a, b);
const subtraction = math.subtract(a, b);
const multiplication = math.multiply(a, b);
const division = math.divide(a, b);
console.log(`Addition: ${a} + ${b} = ${addition}`);
console.log(`Subtraction: ${a} - ${b} = ${subtraction}`);
console.log(
`Multiplication: ${a} * ${b} = ${multiplication}`);
console.log(`Division: ${a} / ${b} = ${division}`);
Note: Run command node script.js , to see output.Output
Addition: 10 + 5 = 15 Subtraction: 10 - 5 = 5 Multiplication: 10 * 5 = 50 Division: 10 / 5 = 2 Basic Arithmetic Operations Using math.evaluateIn this approach, we are using the math.evaluate function from the math.js library to perform basic arithmetic operations. We define two numbers, a and b, and pass arithmetic expressions as strings to math.evaluate. This function evaluates the expressions dynamically, allowing us to compute and obtain the results for addition, subtraction, multiplication, and division. The results are then logged to the console.
Example: The below example performs Basic Arithmetic Operations Using math.evaluate.
JavaScript
// script.js
const math = require("mathjs");
const a = 10;
const b = 5;
const addition = math.evaluate(`${a} + ${b}`);
const subtraction = math.evaluate(`${a} - ${b}`);
const multiplication = math.evaluate(`${a} * ${b}`);
const division = math.evaluate(`${a} / ${b}`);
console.log(`Addition: ${a} + ${b} = ${addition}`);
console.log(`Subtraction: ${a} - ${b} = ${subtraction}`);
console.log(
`Multiplication: ${a} * ${b} = ${multiplication}`);
console.log(`Division: ${a} / ${b} = ${division}`);
Note: Run command node script.js , to see output.Output
Addition: 10 + 5 = 15 Subtraction: 10 - 5 = 5 Multiplication: 10 * 5 = 50 Division: 10 / 5 = 2
|