Q:

String to number in pure JavaScript

When assigning a numerical value to a variable, this code {var x = 1+2+3;} works perfectly but if you if first make it {var x = "1+2+4";} and then later try to use Number(x) to convert it to a number, you get NaN.
Is there a way to to convert the string "1+3+4" into a number and get the answer 8?

A:

Yes, there is, just split the string and add the values to an accumulator using reduce:

let x = "1+3+4";

console.log(x.split("+").reduce((a, b) => parseInt(a) + parseInt(b)));

