How can I test if a mathematical expression is true?
var equation = "1 + 1 = 2";
if (equation === true){
document.write("1 + 1 = 2");
}else{
document.write("1 + 1 =/= 2");
}
For obvious reasons, this code ^^^ doesn’t work, but I just wanted to know if there was a function that would do this; evaluate a mathematical expression then return its verity as a boolean (true/false).
One approach is to use
eval; but note that it requires JavaScript notation, not an ad-hoc notation:This approach has the downside of not being very “safe”, since the
eval‘d string can contain any JavaScript code whatsoever. That greatly restricts the set of situations in which it makes sense to use it.