mathjs
Extensive math library for JavaScript and Node.js with expression parsing, units, matrices, and numeric types.
Official Documentation ↗v15.2.0. Check the official docs for the latest API surface.Installation
npm install mathjspnpm add mathjsMethods
No methods match this filter.
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:
Error(函数页 Throws 表声明抛出 Error) — official text: “### Throws | Type | Description | |—|—| | Error | |” (source: https://mathjs.org/docs/reference/functions/evaluate.html, fetched 2026-11-08T17:53:50.000Z)
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
evalandnew Functionwhich are the main cause of security attacks. Mathjs versions 4 and newer does not use JavaScript’sevalunder 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: *
importandcreateUnitwhich alter the built-in functionality and allow overriding existing functions and units, andreviverwhich 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.
evaluate→ signature from https://mathjs.org/docs/reference/functions/evaluate.html; official text: “## Syntaxmath.evaluate(expr) math.evaluate(expr, scope) math.evaluate([expr1, expr2, expr3, ...]) math.evaluate([expr1, expr2, expr3, ...], scope)”multiply→ signature from https://mathjs.org/docs/reference/functions.html; official text: “| math.multiply(x, y) | Multiply two or more values,x * y. |”matrix→ signature from https://mathjs.org/docs/reference/functions.html; official text: “| math.matrix(x) | Create a Matrix. |”
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.