Horje
How to Perform Basic Arithmetic Operations with math.js?

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 Operation

Step 1: Initialize a Node.js Project

Before 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
Screenshot-2024-07-23-173725

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 Functions

In 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.evaluate

In 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



Reffered: https://www.geeksforgeeks.org


Web Technologies

Related
How to Add Meta Tags in WordPress? How to Add Meta Tags in WordPress?
How To Write Good API Documentation? How To Write Good API Documentation?
How to Evaluate Expressions with math.js? How to Evaluate Expressions with math.js?
Next.js Link Component Next.js Link Component
Next.js Functions: notFound Next.js Functions: notFound

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
19