jslint-error-explanations icon indicating copy to clipboard operation
jslint-error-explanations copied to clipboard

Explain Unexpected '1'

Open cphill02 opened this issue 12 years ago • 1 comments
trafficstars

When converting a string like '9.59' to a number I have always used the syntax: var a = '9.59'; a = a * 1;

JSLint seems to find this method odd and destructive. I thought my method was fairly standard and straight forward. It also happens to be the fastest to execute vs any other method like parseInt or parseFloat or Math.floor

cphill02 avatar Jul 01 '13 21:07 cphill02

Use a plus can easily change convert string to number.

var a = '9.59';
a = +a;

But change a variable type is not good practice. Assign the result number to another variable is much better.

And I think * 1 is a meaning less operation. The formula means nothing will change. So JSLint warns.

othree avatar Jul 02 '13 04:07 othree