Evaluator.js is a small, zero-dependency module for evaluating mathematical expressions.
All major operations, constants, and methods are supported. Additionally, Evaluator.js intelligently reports invalid syntax, such as a misused operator, missing operand, or mismatched parentheses.
Evaluator.js is used by a desktop calculator application of the same name. See a live demo on the website.
Installation
npm install evaluator.js
API
/** * Takes a string and evaluates the result. * * @param {string} expression The string. * * @throws {Error} No input. * @throws {Error} No valid tokens. * @throws {Error} Misused operator: <token>. * @throws {Error} Mismatched parentheses. * @throws {Error} Invalid token: <token>. * @throws {Error} No operations. * @throws {Error} Insufficient arguments for method: <token>. * @throws {Error} Insufficient operands for operator: <token>. * @throws {Error} Division by zero. * @throws {Error}