Search code examples
javascriptnode.jsconstantsjslintjshint

Why does JSHint throw a warning if I am using const?


This is the error I get when using const:

<error line="2" column="1" severity="warning" message="&apos;const&apos; is available in ES6 (use esnext option) or Mozilla JS extensions (use moz)." source="jshint.W104" />

My code looks like this:

const Suites = {
    Spade: 1,
    Heart: 2,
    Diamond: 3,
    Club: 4
};

The code works fine only JSHint is warning me every time.


Solution

  • When relying upon ECMAScript 6 features such as const, you should set this option so JSHint doesn't raise unnecessary warnings.

    /*jshint esnext: true */ (Edit 2015.12.29: updated syntax to reflect @Olga's comments)

    /*jshint esversion: 6 */
    
    const Suites = {
        Spade: 1,
        Heart: 2,
        Diamond: 3,
        Club: 4
    };
    

    This option, as the name suggests, tells JSHint that your code uses ECMAScript 6 specific syntax. http://jshint.com/docs/options/#esversion

    Edit 2017.06.11: added another option based on this answer.

    While inline configuration works well for an individual file, you can also enable this setting for the entire project by creating a .jshintrc file in your project's root and adding it there.

    {
      "esversion": 6
    }