DEV Community

Discussion on: You should stop using `parseInt()`

Collapse
 
coolprofessor profile image
coolprofessor

What about eval()?

Collapse
 
aminmansuri profile image
hidden_dude

eval is dangerous..

eval("alert('you are hacked')")

Collapse
 
coolprofessor profile image
coolprofessor • Edited

I get your point, but you can already run JS commands in the console. Also, you can use str.includes("()").

Thread Thread
 
blackr1234 profile image
blackr1234

However, end users being able to use console to execute any code doesn't necessarily mean that they will want to do it proactively. If you use eval and if the input is harmful, the end user may be passively affected.

Thread Thread
 
coolprofessor profile image
coolprofessor

Potentially, but can't you check the string for functions using 'str.includes("()")'?

Thread Thread
 
blackr1234 profile image
blackr1234 • Edited

If you are referring to checking if the string contains function call by searching for "()", no it won't work because there are way too many scenarios. Consider a case when there are spaces in between the parenthesis, e.g. foo( ) and your code will then allow it to run. It will be better if you only allow whitelisted characters. However, it will still take unnecessary effort and still potentially cause the program to hang (if you are going to search/parse the whole string which can be very long). So just use the built-in functions that work just fine and don't reinvent the wheel, which is something stupid.

Collapse
 
aminmansuri profile image
hidden_dude

Don't do it.

eval() should never be used on user input.

Often parsing strings to Int is done for security reasons. Using eval() would just lead you to code injection and XSS problems.

Don't do it!

Collapse
 
blackr1234 profile image
blackr1234 • Edited

It will be an overkill. There are so many working ways of parsing integers. Why bother using such a dangerous way?