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

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

This is the error I get when using const:

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.

Solutions/Answers:

Solution 1:

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
}

Solution 2:

You can add a file named .jshintrc in your app’s root with the following content to apply this setting for the whole solution:

{
    "esversion": 6
}

James’ answer suggests that you can add a comment /*jshint esversion: 6 */ for each file, but it is more work than necessary if you need to control many files.

Related:  Any way to extend javascript's array.sort() method to accept another parameter?

Solution 3:

I got this same warning when using an export statement. I’m using VS Code and used a similar approach to Wenlong Jiang’s solution.

  1. User Settings
  2. JSHint config
  3. "jshint.options": {} (Edit)
  4. Use double quotes when specifying "esversion"

    Or copy this snippet into User Settings:

    "jshint.options": {
      "esversion": 6,
    }
    

Creating a .jshintrc file isn’t necessary if you want to configure the global jshint settings for your editor

Solution 4:

I spent ages trying to fix this. Every solution talks about ‘setting options’. I don’t know what that means. Finally, I figured it out. You can just include a commented out line at the top of the file /*jshint esversion: 6 */.

Solution

Solution 5:

If you’re using VSCode:

1.

  • Go to preferences -> settings (cmd + ,)
  • Type jshint.options into the search bar
  • Hover over it and click on the pencil icon
  • Its now appended on the right side.
  • Add "esversion": 6 to the options object.

2.

Or simply add this to your user settings:

"jshint.options": {
    "esversion": 6
}

[UPDATE] new vscode settings

  • Go to preferences -> settings (cmd + ,)
  • type jshint into search
Related:  Binding initial/default value of dropdown (select) list

VSCode Settings

  • continue with step 2.

Solution 6:

You can specify esversion:6 inside jshint options object. Please see the image. I am using grunt-contrib-jshint plugin.

enter image description here