How to Parse and Compile Expressions Using math.js?
                                        
                                                                                    
                                                
                                                    Last Updated : 
                                                    23 Jul, 2025
                                                
                                                 
                                                 
                                             
                                                                             
                                                             
                            
                            
                                                                                    
                 Parsing and compiling in  math.js  consists of converting mathematical expressions into a structured format and preparing them for evaluation. This involves parsing the expression to create an abstract syntax tree, compiling this tree into a more efficient form, and then evaluating it with specific variable values.\
Basic Parsing and Compiling 
 In this approach, we are using the  math.js  library to parse and compile a simple arithmetic expression. We first parse the expression using math.parse, then compile it with node.compile, and finally evaluate the compiled expression to get the result. The output is logged to the console. 
 Example:  The below example uses Basic Parsing and Compiling. 
            JavaScript
    const math = require('mathjs');
// Define the expression
const expression = '2 + 3 * 4';
// Parse the expression
const node = math.parse(expression);
// Compile the parsed expression
const compiled = node.compile();
// Evaluate the compiled expression
const result = compiled.evaluate();
// Log the result
console.log('Result:', result);
 Output
Result: 14
Parsing and Compiling with Variables 
 In this approach, we are using  math.js  library to parse and compile an arithmetic expression containing variables. After parsing and compiling the expression, we define different sets of variable values (scopes) and evaluate the compiled expression with these scopes. The results for each scope are then logged to the  console  . 
 Example:  The below example uses Parsing and Compiling with Variables. 
            JavaScript
    const math = require('mathjs');
// Define the expression with variables
const expression = 'a * b + c';
// Parse the expression
const node = math.parse(expression);
// Compile the parsed expression
const compiled = node.compile();
// Define different scopes with variable values
const scope1 = { a: 2, b: 3, c: 4 };
const scope2 = { a: 5, b: 6, c: 7 };
// Evaluate the compiled expression with each scope
const res1 = compiled.eval(scope1);
const res2 = compiled.eval(scope2);
console.log('Result with scope1:', res1);  
console.log('Result with scope2:', res2);  
 Output
Result with scope1:   10
Result with scope2:  37
Parsing and Compiling with Functions 
 In this approach, we are using the  math.js library  to parse and compile an expression that includes a custom function. We first create an instance of  math.js  with all functions using create(all), then import a custom square function. After parsing and compiling the expression that uses this custom function, we evaluate it with given variable values and log the result. 
 Example:  The below example uses Parsing and Compiling with Functions. 
            JavaScript
    const { create, all } = require('mathjs');
const math = create(all);
math.import({
    square: function (x) {
        return x * x;
    }
}, { override: true });
const expression = 'square(x) + y';
const node = math.parse(expression);
const compiled = node.compile();
const scope = { x: 3, y: 4 };
const result = compiled.evaluate(scope);
console.log('Result:', result); 
 Output
Result: 13
                                
                                
                            
                                                                                
                                                            
                                                    
                                                
                                                        
                            
                        
                                                
                        
                                                                                    
                                                                Explore
                                    
                                        JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics