Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!


Do not use {a} as a constructor

When do I get this error?

JSLint and JSHint will throw the "Do not use {a} as a constructor" error when they encounter a call to String, Number, Boolean, Math or JSON preceded by the new operator. In the following example we attempt to assign some values to variables by invoking these functions as constructors:

Why do I get this error?

This error is raised to highlight a bad practice and a piece of code that may not work as you intend it to. It can also highlight a possible fatal JavaScript error. Your code could run without error if you do not change it, but could be confusing to other developers and could also behave in unexpected ways.

The String, Number and Boolean constructor functions return objects of type String, Number and Boolean, which is rarely what you want. Usually, you want literal string, number or boolean values, because strictly comparing an object to a literal will always return false.

In the case of these objects, to fix this error, use literal values rather than their corresponding wrapper objects:

Note that this does not cause you to lose any functionality, since literal values are internally cast to instances of the appropriate type when you call a method on them. Also note that you are free to use these functions to perform type conversions i.e. by invoking them without the new operator:

The case is a little different for the Math and JSON objects. These two objects are not functions, and cannot be constructed. Attempts to instantiate them will result in a TypeError. If you're trying to serialize an object into a JSON string, you need to use the JSON.stringify method instead:

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 W053. This means you can tell JSHint to not issue this warning with the /*jshint -W053 */ directive.


James Allardice

This article was written by James Allardice, an enthusiastic young JavaScript developer at Global Personals (we're looking for developers so please apply). He is passionate about writing clean, maintainable JavaScript and uses JSHint every day to help achieve this.

You can follow him on Twitter, fork him on GitHub and find him on Google+.


Proceeds generated by this site are donated to help advance other open source projects



comments powered by Disqus