{"id":15504,"library":"algebra.js","title":"Algebra.js Symbolic Equation Solver","description":"Algebra.js is a JavaScript library designed for building, displaying, and solving algebraic equations. As of version 0.2.6, it provides core functionalities for creating `Fraction` objects for precise arithmetic, `Expression` objects to represent algebraic expressions, and `Equation` objects to encapsulate equalities. A key differentiator is its ability to parse string representations of expressions and equations, enabling convenient construction from human-readable input. The library also includes methods for symbolic simplification, evaluation, and solving linear, quadratic, cubic, and quartic equations, along with a `toTex()` method for LaTeX output. It primarily targets Node.js and browser environments using CommonJS module syntax. The release cadence is infrequent, with recent updates focusing on bug fixes and stability within the 0.2.x series.","status":"maintenance","version":"0.2.6","language":"javascript","source_language":"en","source_url":"https://github.com/nicolewhite/algebra.js","tags":["javascript","algebra","math","fractions","equations","expressions","latex"],"install":[{"cmd":"npm install algebra.js","lang":"bash","label":"npm"},{"cmd":"yarn add algebra.js","lang":"bash","label":"yarn"},{"cmd":"pnpm add algebra.js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This library is a CommonJS module. The `algebra` object is the main export, containing all constructors and utility methods.","wrong":"import algebra from 'algebra.js';","symbol":"algebra","correct":"const algebra = require('algebra.js');"},{"note":"`Expression` is a constructor exposed as a property of the main `algebra` export, not a named export itself.","wrong":"const { Expression } = require('algebra.js');\n// or\nimport { Expression } from 'algebra.js';","symbol":"Expression","correct":"const algebra = require('algebra.js');\nconst expr = new algebra.Expression('x');"},{"note":"`Equation` is a constructor exposed as a property of the main `algebra` export, not a named export itself.","wrong":"const { Equation } = require('algebra.js');\n// or\nimport { Equation } from 'algebra.js';","symbol":"Equation","correct":"const algebra = require('algebra.js');\nconst eq = new algebra.Equation(expr, 4);"},{"note":"`Fraction` is a constructor exposed as a property of the main `algebra` export, used for precise fractional arithmetic.","wrong":"const { Fraction } = require('algebra.js');\n// or\nimport { Fraction } from 'algebra.js';","symbol":"Fraction","correct":"const algebra = require('algebra.js');\nconst frac = new algebra.Fraction(1, 2);"},{"note":"The `parse` utility method is directly available on the main `algebra` object for parsing strings into expressions or equations.","wrong":"const { parse } = require('algebra.js');\n// or\nimport { parse } from 'algebra.js';","symbol":"parse","correct":"const algebra = require('algebra.js');\nconst exp = algebra.parse(\"x^2 + 4 * x + 4\");"}],"quickstart":{"code":"const algebra = require('algebra.js');\n\n// Create an expression\nlet expr = new algebra.Expression(\"x\");\nexpr = expr.subtract(3);\nexpr = expr.add(\"x\");\n\nconsole.log(expr.toString()); // Expected: 2x - 3\n\n// Create an equation from the expression\nconst eq = new algebra.Equation(expr, 4);\n\nconsole.log(eq.toString()); // Expected: 2x - 3 = 4\n\n// Solve the equation for 'x'\nconst x = eq.solveFor(\"x\");\n\nconsole.log(\"x = \" + x.toString()); // Expected: x = 7/2\n\n// Use the parser to create an equation directly from a string\nconst quadraticEq = algebra.parse(\"x^2 + 4 * x + 4 = 0\");\nconsole.log(quadraticEq.toString()); // Expected: x^2 + 4x + 4 = 0","lang":"javascript","description":"Demonstrates creating expressions, equations, displaying them, solving for a variable, and using the string parser."},"warnings":[{"fix":"Update to version 0.2.0 or later and adjust logic to handle `Fraction(1, 1)` instead of `undefined` for infinite solutions.","message":"In versions prior to 0.2.0, solving single-variable equations with an infinite number of solutions would return `undefined`. This behavior changed in 0.2.0 to return `Fraction(1, 1)`.","severity":"gotcha","affected_versions":"<0.2.0"},{"fix":"Upgrade to version 0.2.4 or newer to ensure correct solving of quadratic equations under these conditions.","message":"Versions prior to 0.2.4 contained a bug where solving quadratic equations with irrational solutions when the 'a' coefficient was greater than 1 yielded incorrect results. Relying on such solutions in older versions would have produced wrong output.","severity":"gotcha","affected_versions":"<0.2.4"},{"fix":"Upgrade to version 0.2.2 or newer to benefit from improved parser robustness and correct interpretation of negative numbers and adjacent parentheses.","message":"The parser in versions prior to 0.2.2 incorrectly handled negative numbers (throwing an error) and misinterpreted adjacent parentheses. Code relying on previous buggy parser behavior or error handling for these cases will need adjustment.","severity":"gotcha","affected_versions":"<0.2.2"},{"fix":"Update to version 0.2.1 or later to ensure `Expression.simplify()` consistently reduces expressions to their single simplest constant form.","message":"`Expression.simplify()` in versions prior to 0.2.1 would sometimes fail to fully simplify expressions consisting only of unsimplified constants, reducing them to two constants instead of a single one. This could lead to unexpected unsimplified numerical results.","severity":"gotcha","affected_versions":"<0.2.1"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[],"ecosystem":"npm"}