UNPKG

1.34 kBMarkdownView Raw
1<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
3# Function parse
4
5Parse an expression. Returns a node tree, which can be evaluated by
6invoking node.evaluate().
7
8Note the evaluating arbitrary expressions may involve security risks,
9see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information.
10
11
12## Syntax
13
14```js
15math.parse(expr)
16math.parse(expr, options)
17math.parse([expr1, expr2, expr3, ...])
18math.parse([expr1, expr2, expr3, ...], options)
19```
20
21### Parameters
22
23Parameter | Type | Description
24--------- | ---- | -----------
25`expr` | string &#124; string[] &#124; Matrix | Expression to be parsed
26`options` | {nodes: Object&lt;string, Node&gt;} | Available options:</br>- `nodes` a set of custom nodes
27
28### Returns
29
30Type | Description
31---- | -----------
32Node &#124; Node[] | node
33
34
35## Examples
36
37```js
38const node1 = math.parse('sqrt(3^2 + 4^2)')
39node1.compile().evaluate() // 5
40
41let scope = {a:3, b:4}
42const node2 = math.parse('a * b') // 12
43const code2 = node2.compile()
44code2.evaluate(scope) // 12
45scope.a = 5
46code2.evaluate(scope) // 20
47
48const nodes = math.parse(['a = 3', 'b = 4', 'a * b'])
49nodes[2].compile().evaluate() // 12
50```
51
52
53## See also
54
55[evaluate](evaluate.md),
56[compile](compile.md)