API Reference

mathjs

Extensive math library for JavaScript and Node.js with expression parsing, units, matrices, and numeric types.

Official Documentation ↗
CompatibilityThis documentation covers version v15.2.0. Check the official docs for the latest API surface.

Installation

npmnpm install mathjs
pnpmpnpm add mathjs

Methods

Overview

mathjs is a broad math library for JavaScript and Node.js with expression parsing, units, matrices, and multiple numeric types. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • evaluate — Parses and evaluates a mathematical expression against an optional scope.
  • multiply — Multiplies numbers, matrices, or units with type-aware behavior.
  • matrix — Builds a matrix object that supports linear algebra operations.

Example

const { evaluate } = require('mathjs');

console.log(evaluate('2 + 3 * 4'));
console.log(evaluate('a^2 + b', { a: 3, b: 1 }));

Notes

It parses expressions without eval, but untrusted input should still be limited via a restricted instance. Importing a custom instance also trims bundle size.

Verification

Evaluate an expression with a scope variable and confirm the numeric result matches a hand-computed value.

Errors

The official API reference declares the following error types:

Security

Official security statements captured from the package documentation:

  • 表达式解析器提供沙箱环境,但存在未知漏洞风险,服务端执行任意表达式需谨慎 — “A user could try to inject malicious JavaScript code via the expression parser. The expression parser of mathjs offers a sandboxed environment to execute expressions which should make this impossible. It’s possible though that there are unknown security vulnerabilities, so it’s important to be caref” (source: https://mathjs.org/docs/expressions/security.html, fetched 2026-11-08T17:53:50.000Z)
  • 解析器阻止访问 eval 与 new Function;mathjs 4+ 不再使用 eval — “The parser actively prevents access to JavaScripts internal eval andnew Function which are the main cause of security attacks. Mathjs versions 4 and newer does not use JavaScript’s eval under the hood.” (source: https://mathjs.org/docs/expressions/security.html, fetched 2026-11-08T17:53:50.000Z)
  • 可通过 import + override 禁用高风险函数(import/createUnit/reviver/evaluate/parse/simplify/derivative/resolve) — “There is a small number of functions which yield the biggest security risk in the expression parser: * import and createUnit which alter the built-in functionality and allow overriding existing functions and units, and reviver which parses values into class instances. * evaluate, parse, `s” (source: https://mathjs.org/docs/expressions/security.html, fetched 2026-11-08T17:53:50.000Z)

API reference evidence

Fetched from official sources on 2026-11-08T17:53:50.000Z via WebFetch https://mathjs.org/docs/reference/functions/evaluate.html.

FAQ

Is evaluating user input with mathjs safe?
mathjs parses expressions itself rather than using eval, but untrusted input should still be constrained and run with a limited scope.
How do I restrict available functions?
Use the create(...) factory to build an instance containing only the functions you import, limiting what expressions can call.
Does mathjs support units?
Yes. It understands unit suffixes so expressions like 5 cm + 2 inch produce a converted result.