2024-08-26 13:59:54 +02:00

66 lines
2.0 KiB
Markdown

---
layout: default
---
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
<h1 id="function-evaluate">Function evaluate <a href="#function-evaluate" title="Permalink">#</a></h1>
Evaluate an expression.
The expression parser does not use JavaScript. Its syntax is close
to JavaScript but more suited for mathematical expressions.
See [https://mathjs.org/docs/expressions/syntax.html](https://mathjs.org/docs/expressions/syntax.html) to learn
the syntax and get an overview of the exact differences from JavaScript.
Note the evaluating arbitrary expressions may involve security risks,
see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information.
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
```js
math.evaluate(expr)
math.evaluate(expr, scope)
math.evaluate([expr1, expr2, expr3, ...])
math.evaluate([expr1, expr2, expr3, ...], scope)
```
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
Parameter | Type | Description
--------- | ---- | -----------
`expr` | string &#124; string[] &#124; Matrix | The expression to be evaluated
`scope` | Object | Scope to read/write variables
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
Type | Description
---- | -----------
* | The result of the expression
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
Type | Description
---- | -----------
Error |
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
```js
math.evaluate('(2+3)/4') // 1.25
math.evaluate('sqrt(3^2 + 4^2)') // 5
math.evaluate('sqrt(-4)') // 2i
math.evaluate(['a=3', 'b=4', 'a*b']) // [3, 4, 12]
let scope = {a:3, b:4}
math.evaluate('a * b', scope) // 12
```
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
[parse](parse.html),
[compile](compile.html)