Parsing mathematical expressions

Kontrast offers the function kontrast.parseExpression which allows to parse and evaluate mathematical expressions from strings. As JavaScript does not provide methods to deal natively with complex numbers, this function can be used to perform calculations with complex numbers.

The syntax for the mathematical expressions is explained in this separate documentation article.

Interactive code example


The function is called as kontrast.parseExpression(options), where options is an object with the following properties:

  • expression: string
    The mathematical expression.
  • numberType: 'real' | 'complex' (default value: 'real')
    This switch determines whether the calculation is using real numbers or complex numbers both internally and as a result type.


  • evaluate: function
    A function that actually performs the calculation. The function accepts a single object that contains the variable names as keys and numeric data as value.
  • variables: array
    An array of strings containing the names of all variables used in the expression.
  • element: HTMLElement
    A HTML element containing the render of the expression.