tash
tash

Reputation: 941

JavaScript plus unary operator returns NaN

When trying to convert a string to number in a browser console:

let a="3,437,286"
+a

returns NaN. Do you see what am I missing here?

Upvotes: 0

Views: 258

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

That string isn't a proper number. Maybe if the string always has numbers with commas, you can remove the commas and try to check it:

let a = "3,437,286";
console.log(+a);                    // NaN
console.log(+a.replace(/,/g, ""));  // 3437286

Upvotes: 1

Related Questions