JS Formula parser & Demo

Demo page for my Formula parser library: Parses strings into evaluatable mathematical expressions. Check page source for all relevant source code :-)
Note: This demo uses jQuery and the flot library.
Note 2: This page can only be viewed with a real browser (so Firefox or Chrome).

Known expressions:
Enter formula
Evaluate single value

Library Usage

For those interested in using the parsing library, just follow those instructions:

1. Get the Formula JS-Object

Just include the fparser.js in your HTML header:
<script src="fparser.js"></script>
This defines a global Formula object constructor.

2. Create a Formula object instance

Simple usage:
//Step 1: create a formula object by passing a formula string:
var myFormulaStr = '2^x';
var fObj         = new Formula(myFormulaStr);

// Step 2: evaluate the formula, delivering a value object for each unknown entity:
var result = fObj.evaluate({x: 3}); // result = 8

// or deliver multiple value objects to return multiple results:
var results = fObj.evaluate([{x: 2},{x: 4},{x: 8}]); // results = [4,16,256]

3. Advanced usage

Using multiple unknown variables:
var fStr = 'a*x^2 + b*x + c';
var fObj = new Formula(fStr);

// Just pass a value object containing a value for each unknown variable:
var result = fObj.evaluate({a:2,b:-1,c:3,x:3}); // result = 18
Using user-defined functions:
var fObj = new Formula('sin(inverse(x))');

//Define the function(s) on the Formula object, then use it multiple times:
fObj.inverse = function(value){
    return 1/value;
var results = fObj.evaluate({x: 1,x:2,x:3});

// Or pass it in the value object, and OVERRIDE an existing function:
var result = fObj.evaluate({
	x: 2/Math.PI, 
	inverse: function(value){
		return -1*value;

If defined in the value object AND on the formula object, the Value object has the precedence

// result = 1;
Fetching all used variables:
// Get all used variables in the order of their appereance:
var f4 = new Formula('x*sin(PI*y) + y / (2-x*a) + z');
console.log(f4.getVariables()); // ['x','y','a','z']