0

As written in a string, I want to convert the "first" variable into a Number. In order to perform further calculations. Code is below:

let first = "34+4-5";
console.log(first);
console.log(typeof(first));

let second = Number(first)
console.log(second);
console.log(typeof(second))

let third = parseInt(first);
console.log(third);
console.log(typeof(third));

But I get Output

34+4-5  
string


NaN  
number

34
number

I want to calculate it further (34+4-5), so that answer will be 33

  • you can use `eval( "34+4-5")` but its risky. refer here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval – gorak Oct 04 '22 at 10:49
  • Look at https://stackoverflow.com/questions/18459798/javascript-regular-expression-to-parse-a-formula – Jay Buckman Oct 04 '22 at 10:56

0 Answers0