A dot following a number can be confused with a decimal point

When do I get this error?

The "A dot following a number can be confused with a decimal point" error is thrown when JSHint encounters a numeric literal containing a decimal point as the left-hand-side of a member expression. In the following example we attempt to assign the string representation of a number to a variable:

var a = 5.4.toString();

Note that this is slightly different to closely related "A trailing decimal point can be consued with a dot" error, although JSLint will use that message in this situation too.

Why do I get this error?

This error is raised to highlight a potentially confusing piece of code. Your script will run without error if you do not change it, but it could be confusing to other developers, especially at first glance.

Since a number can only contain a single decimal point the parser is able to determine that any subsequent occurences of the character after a numeric literal can only be intended as a member operator. In other words the ambiguity of the . character is removed. However the construct can appear confusing at first glance.

The best solution in this case is to wrap the number in parentheses:

var a = (5.4).toString();

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W005. This means you can tell JSHint to not issue this warning with the /*jshint -W005 */ directive.

About the author

James Allardice

This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.