Nerdamer Scripting

Nerdamer Scripting

Nerdamer Scripting is the compact programming layer available inside text passed to nerdamer(...). Use it when a single mathematical expression is not enough and you need assignments, user-defined functions, conditions, loops, or local scope.

Not JavaScript: the scripting layer is evaluated by the library's parser. For ordinary mathematical expressions and functions, see Nerdamer Notation. For methods and imported APIs, see the JavaScript / TypeScript API.

Notation and scripting side by side


import nerdamer from 'nerdamer';

// Nerdamer Notation: one mathematical expression

const expression = nerdamer('factor(x^2-1)');

// Nerdamer Scripting: define and use a function

const script = nerdamer(`
square(x):=x^2;
square(6)
`);

User-defined functions

Define a symbolic function in scripting with f(x):=..., or define it from application code with nerdamer.setFunction(...). See User-defined functions for both forms and how they relate.

For larger workflows built entirely in the scripting language, see the Scripting examples.

Scripting reference

The entries below are the parser functions classified as scripting by the generated documentation data.

Assignment

Use : to assign a parser variable. Assignments are retained by the parser and can affect later calls until they are removed.


a:5;
b:a+2;
b

Use unassign(a) to remove one stored assignment, nerdamer.clearVars() from application code to clear them all, or let(...) for temporary local bindings.

Statement separators

Newlines are formatting whitespace, not statement separators. Use ; between multiple statements passed in one call.


f(x):=x^2+1;
f(12)

Blocks and local scope

block(...) evaluates expressions in sequence. let(...) creates temporary local bindings while its body is evaluated.


block(a:2, b:3, a+b)
let(x, 2, x^3)

Conditions and loops

if, while, and for let a script choose or repeat expressions.


if(x>0, x, -x)

let(k,0,block(
  while(3-k, k:k+1),
  k
))

Control flow

return, break, and continue control how blocks, functions, and loops finish. Their individual reference pages describe where each form applies.