Question
How can I disable an ESLint rule for a particular line of JavaScript, similar to using JSHint ignore comments?
/* jshint ignore:start */
$scope.someVar = ConstructorFunction();
/* jshint ignore:end */
Is there an ESLint equivalent that suppresses a linting warning for only the intended line or small section of code?
Short Answer
ESLint supports special directive comments that can disable rules for the next line, the current line, a block of code, or an entire file. The safest option is usually eslint-disable-next-line with the specific rule name, because it keeps the suppression narrow and visible.
Concept
ESLint reports code that violates rules configured for a project. Occasionally, code must intentionally break a rule: perhaps an external API requires a naming style, legacy code cannot yet be changed, or a safe exception is needed.
Instead of disabling linting everywhere, ESLint lets you add directive comments. A directive tells ESLint where a rule should temporarily stop reporting problems.
For a single following line, use:
// eslint-disable-next-line rule-name
For example, if ConstructorFunction is intentionally provided by a global script and ESLint reports no-undef:
// eslint-disable-next-line no-undef
$scope.someVar = ConstructorFunction();
Naming the exact rule is important. It documents the reason for the exception and prevents unrelated lint rules from being silently ignored.
Mental Model
Think of ESLint as a spelling and safety checker for code. A directive comment is a small, temporary permission slip.
eslint-disable-next-lineis permission for one task.eslint-disableis permission for a short period.eslint-enableends that permission.
Give permission only where it is needed. A permission slip for one line is much safer than turning off the checker for an entire file.
Syntax and Examples
Use an ESLint directive comment immediately before the code it affects.
Disable one named rule on the next line
// eslint-disable-next-line no-console
console.log("Debugging checkout flow");
Only the no-console warning for the next line is suppressed.
Disable all ESLint rules on the next line
// eslint-disable-next-line
legacyLibrary.doSomething();
This works, but it is usually better to specify a rule name. Otherwise, new problems on that line can be hidden.
Disable a rule for a small block
/* eslint-disable no-console */
console.log("Starting import");
console.log("Import complete");
/* eslint-enable no-console */
The no-console rule is disabled between the two comments and enabled again afterward.
Disable a rule on the same line
console.();
Step by Step Execution
Consider this code in a project where no-console is enabled:
function calculateTotal(price, quantity) {
// eslint-disable-next-line no-console
console.log({ price, quantity });
return price * quantity;
}
- ESLint reads the function declaration. There is no
no-consoleissue yet. - ESLint reads the directive comment.
- The comment tells ESLint not to report the
no-consolerule on the next line only. - ESLint checks
console.log({ price, quantity }), but suppresses itsno-consolereport. - ESLint checks the
returnstatement normally. - If another
console.logappears later, ESLint reports it because the directive has already expired.
function calculateTotal(price, quantity) {
// eslint-disable-next-line no-console
console.log({ price, quantity });
console.log();
price * quantity;
}
Real World Use Cases
- Temporary debugging: Allow one
console.logwhile investigating a production-only issue. - Third-party globals: Suppress
no-undeffor a value loaded by an older external script while the integration is being modernized. - Framework or API constraints: Permit a required parameter name that violates a naming convention.
- Migration work: Isolate a known exception while incrementally moving legacy code to new project rules.
- Generated or compatibility code: Suppress a narrowly scoped rule when changing the generated code is not practical.
A directive should describe an intentional exception, not replace fixing ordinary code-quality problems.
Real Codebase Usage
In maintained codebases, developers usually follow these patterns:
Prefer a narrow, named suppression
// eslint-disable-next-line no-console -- Needed while diagnosing payment-provider responses.
console.log(providerResponse);
The rule name and reason make code review easier.
Prefer fixing configuration for known globals
If a value is genuinely global throughout an application, configure it instead of adding repeated no-undef suppressions. For example, in an ESLint flat config:
export default [
{
languageOptions: {
globals: {
ConstructorFunction: "readonly"
}
}
}
];
Then code can use the global without a directive:
$scope.someVar = ConstructorFunction();
Use guard clauses instead of suppressing valid warnings
If ESLint warns about unsafe or unclear code, restructure it when possible:
function getDisplayName(user) {
if (!user) {
;
}
user.;
}
Common Mistakes
Putting eslint-disable-next-line after the line
This does not disable the line above it:
console.log("Debug");
// eslint-disable-next-line no-console
Place it directly before the target line:
// eslint-disable-next-line no-console
console.log("Debug");
Disabling every rule unnecessarily
// eslint-disable-next-line
console.log("Debug");
This can hide multiple unrelated problems. Prefer:
// eslint-disable-next-line no-console
console.log("Debug");
Forgetting to re-enable a block rule
/* eslint-disable no-console */
console.log("Debug");
.();
Comparisons
| Approach | Scope | Best use | Main risk |
|---|---|---|---|
eslint-disable-next-line rule-name | One following line | A deliberate one-line exception | Very low when a rule is named |
eslint-disable-line rule-name | Current line | Inline code where a preceding comment is awkward | Can make long lines harder to read |
eslint-disable / eslint-enable | A code block | A short legacy or generated section | Forgetting to re-enable it |
/* eslint-disable */ | Rest of file or until enabled | Rare special cases | Hides too many problems |
| ESLint configuration |
Cheat Sheet
// Disable one rule for the next line
// eslint-disable-next-line no-console
console.log("Debug");
// Disable all rules for the next line (avoid when possible)
// eslint-disable-next-line
someLegacyCall();
// Disable one rule for the current line
console.log("Debug"); // eslint-disable-line no-console
// Disable a rule for a block
/* eslint-disable no-console */
console.log("Start");
console.log("End");
/* eslint-enable no-console */
// Disable multiple rules for the next line
// eslint-disable-next-line no-console, no-alert
console.log("Debug");
alert("Done");
- Put
eslint-disable-next-lineimmediately before the affected line. - Name the specific rule whenever possible.
- Keep disabled blocks small.
- Add a short reason for non-obvious exceptions.
- Configure genuine project-wide globals or conventions instead of suppressing the same warning repeatedly.
FAQ
How do I disable ESLint for one line?
Put // eslint-disable-next-line rule-name directly above the line. For example:
// eslint-disable-next-line no-console
console.log("Debug");
Can I disable multiple ESLint rules on one line?
Yes. Separate rule names with commas:
// eslint-disable-next-line no-console, no-alert
console.log("Debug");
What is the ESLint equivalent of jshint ignore:start and ignore:end?
Use /* eslint-disable rule-name */ before the section and /* eslint-enable rule-name */ after it.
Should I omit the ESLint rule name?
Usually no. Omitting it disables all rules in the selected scope, which can hide unrelated errors.
Why does eslint-disable-next-line not work?
Check that the comment is immediately before the target line, that the rule name is correct, and that ESLint is actually running on the file.
Should I disable no-undef for a global variable?
Mini Project
Description
Create a small order-processing function that includes intentional debug logging. Practice suppressing no-console for exactly one line while leaving ESLint active everywhere else.
Goal
Write a function that calculates an order total and logs one diagnostic object without triggering the no-console ESLint rule for that log statement.
Requirements
- Create a
calculateOrderTotalfunction that accepts an array of item prices. - Return the sum of all prices.
- Log the item count and calculated total once.
- Disable only the
no-consolerule for that logging line. - Do not disable ESLint for the entire file or function.
Keep learning
Related questions
Abort Ajax Requests with jQuery jqXHR.abort()
Learn how to cancel an in-progress jQuery Ajax request with jqXHR.abort(), handle abort status safely, and avoid stale UI updates.
Access the Correct this Inside a JavaScript Callback
Learn why JavaScript this changes in callbacks and how to preserve an object context using bind, arrow functions, and event handler patterns.
Add Key-Value Pairs to JavaScript Objects
Learn how to add key-value pairs to JavaScript objects with dot and bracket notation, dynamic keys, examples, and common mistakes.