Syed Rasheed
Syed Rasheed

Reputation: 559

`parseInt` is not working as expected when special characters exists

i have var value="10+10" when i try to convert this using parseInt(value) to an int it is giving me NaN. Is their any option to convert a string if it has special characters in it? The Result shoud be 20 or simply 10+10

Upvotes: 0

Views: 500

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41447

you can use eval to evaluate string operations. since parseInt doesn't recognize characters like + it will return the numbers until special characters.

as a example

(parseInt("10+10") print 10 and

(parseInt("100+10") print 100 and

 
console.log(parseInt("10+10"))
console.log(parseInt("100+10"))

console.log(eval("10+10"))
console.log(eval("10*10"))

Upvotes: 1

Related Questions