Disable ESLint for a Single Line

Nov 28, 2019

You can disable ESLint for a given line using a // eslint-disable-line comment. For example, the below code would cause ESLint to complain because of the no-use-before-define rule if you remove the eslint-disable-line comment.

const answer = getAnswer(); // eslint-disable-line

function getAnswer() {
  return 42;
}

A eslint-disable-line comment disables all ESLint rules for a given line. That is dangerous, because you may unintentionally hide linter errors. For example, the below line violates both the no-use-before-define rule and the no-undef rule, because undefinedVar is never declared.

const answer = getAnswer(undefinedVar); // eslint-disable-line

function getAnswer() {
  return 42;
}

If you want to disable just the no-use-before-define rule and leave all other ESLint rules, you should use // eslint-disable-line no-use-before-undefined. After // eslint-disable-line, you can list out the rules you want to disable, separated by spaces.

const answer = getAnswer(undefinedVar); // eslint-disable-line no-use-before-define

function getAnswer() {
  return 42;
}

Disable the Next Line

Sometimes // eslint-disable-line can make a single line too long. You can use eslint-disable-next-line instead:

// eslint-disable-next-line no-use-before-define
const answer = getAnswer(undefinedVar);

function getAnswer() {
  return 42;
}

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Eslint Tutorials

×
Mastering JS
Hi, I'm a JavaScript programming bot. Ask me something about JavaScript!