math" where we could use functions and expressions exactly as they are im math
JavaScript has functions but they do not have the same properties as functions in math.
For example, if we have y = 5x in math, it also means that x = y/5. But we do not have this in JavaScript: we cannot derive x from "const y = x => 5 * x". And yet function in math is an expression that conveys the dependence of 2+ variables on each other without emphasizing which one is "primary".
I would suggest we could have "use math" mode (the same way we have "use strict" mode), where we could do the following stuff:
function one (x, y): "x**2 + 7 * y - 5 = 0";
one(5) // -2.857142857142857 Why? Since we have value of x, we need to find value of y. 5**2 + 7 * y - 5 = 0 => 7 * y = -20 => y = -20/7 => -2.857142857142857
one(, -1) // [ -3.4641016151377544, 3.4641016151377544 ] . Here we have value of y, so we need to find value of x. x**2 - 7 - 5 = 0 => x**2 = 12 => x = +-Math.sqrt(12) => [ -3.4641016151377544, 3.4641016151377544 ]
I don't see any bona fide use case of having a math mode in JavaScript. Given an equation with variables x and y, you can always create functions which solve for x and y as follows.
If you're worried about solveForX and solveForY not representing the same equation, then you can always write unit tests and use property-based testing to check whether solveForX and solveForY are isomorphic.
// p is the precision. It should be an integer.
// It should satisfy the constraint, 1 ≤ p ≤ 52.
const eq = (x, y, p) => Math.abs(x - y) < 2 ** -p;
// Choose the largest value for p that makes the unit tests pass.
const property = (x) => eq(solveForX(solveForY(x)), x, 51);
One of the variables will need to be made the single source of truth. For example, we can make x the single source of truth. When we update x, we simply change the state of the machine to the new value of x. When we update y, we change the state of the machine to solveForX(y) where y is the new value of y and solveForX is defined as follows.
const solveForX = (y) => y / 8;
P.S. I would like to know why you want to have a math mode in JavaScript. What problem are you trying to solve which requires you to have a math mode in JavaScript? Right now, I doubt you actually need a math mode to solve your problem. So, I believe that your proposal to add math mode to JavaScript is a classic case of an XY problem.