DEV Community

Discussion on: eval() and Function() in JavaScript

Collapse
 
king11 profile image
Lakshya Singh

what would be an alternative for lets say if am building a calculator and i want to parse the user input

Collapse
 
nombrekeff profile image
Keff

It depends on the scope of the calculator, if it's a simple one you could implement it yourself, it's quite simple actually. If the calculator is a bit more complex you could use some expression library, like github.com/silentmatt/expr-eval or mathjs.org/. Either way, I recommend checking if they have any security considerations.

Take into account, that you might not need to worry about security as much on a simple calculator, as on a more serious or sensitive application (has user's info, banking details, passwords, etc...). You might be ok using eval in this scenario.

This is something you have to evaluate yourself, is your app/project sensitive? can a malicious actor take advantage of it and harm or access hidden information? etc...