Introduction to ESLint Rules

Explore common ESLint rules and recommended configurations through practical examples for frontend beginners.

🎯 Introduction

ESLint is one of the most widely used static analysis tools in frontend development. It helps improve code quality and keep a team's code style consistent. This article explores common ESLint rules through practical examples, helps you get started with rule configuration, and discusses configuration choices for different projects. These skills help you avoid errors, maintain consistency, and work more efficiently.


✨ Understanding Common Rules

ESLint offers a wide range of rules. We will group them into three categories: syntax, style, and error prevention. Each category includes examples to explain how the rules work.


🔍 Syntax Rules

These rules help you follow JavaScript conventions and avoid misused syntax or discouraged patterns.

no-unused-vars: Disallow Unused Variables

This rule identifies variables that are declared but never used, helping you avoid redundant code.

// ❌ Incorrect
const foo = 1;
function demo() {
  const bar = 2; // bar is unused, so ESLint reports an error
}
// ✅ Correct
const foo = 1;
function demo() {
    console.log(foo);
}

no-undef: Disallow Undefined Variables

This helps prevent runtime errors caused by typos or missing variable imports.

// ❌ Incorrect
console.log(undeclaredVar);
// ✅ Correct
const declaredVar = 'hello';
console.log(declaredVar);

eqeqeq: Require Strict Equality === and Inequality !==

This prevents logic errors caused by implicit type conversion.

// ❌ Incorrect
if (a == 10) { ... }
// ✅ Correct
if (a === 10) { ... }
This rule is especially useful for JavaScript beginners because it helps avoid unexpected type conversion errors.

🎨 Style Rules

Style rules keep your code consistent and tidy, making it easier to read and maintain.

indent: Enforce Consistent Indentation, with a Default of 2 Spaces

Consistent indentation makes the structure of your code easy to see.

// ❌ Incorrect
function test() {
    console.log('indent error');
  }
// ✅ Correct
function test() {
    console.log('indent correct');
}

semi: Require or Disallow Semicolons

Whether you prefer semicolons or not, ESLint can enforce a consistent choice.

// ❌ Incorrect (inconsistent semicolons)
const a = 1
const b = 2;
// ✅ Correct (semicolons throughout)
const a = 1;
const b = 2;
// ❌ Incorrect
const str = "hello";
// ✅ Correct
const str = 'hello';

comma-dangle: Require or Disallow Trailing Commas in Objects, Arrays, and Function Parameters

This helps reduce unnecessary changes in git diffs.

// ❌ Incorrect
const arr = [1, 2, 3];
// ✅ Correct (with a trailing comma)
const arr = [1, 2, 3];
Style rules help team members follow the same conventions and reduce disagreements during code review.

🚫 Error Prevention Rules

These rules focus on preventing potential logic errors and bugs that are difficult to spot.

no-console: Disallow console.log

Debugging output is generally not intended for production.

// ❌ Incorrect
console.log('Debug information');
// ✅ Correct
// Remove or comment out logging when debugging is complete

no-debugger: Disallow debugger

This prevents debugging statements from accidentally reaching production.

no-duplicate-case: Disallow Duplicate case Labels

This mistake is easy to make in complex switch statements.

// ❌ Incorrect
switch (x) {
  case 1:
    break;
  case 1: // Duplicate case
    break;
}
// ✅ Correct
switch (x) {
    case 1:
        break;
    case 2:
        break;
}

no-fallthrough: Disallow Fallthrough Between case Clauses in switch Statements

This prevents a missing break from unintentionally executing the next case clause.

// ❌ Incorrect
switch (x) {
  case 1:
    doSomething();
  case 2:
    doSomethingElse(); // Runs unintentionally
}
// ✅ Correct
switch (x) {
    case 1:
        doSomething();
        break;
    case 2:
        doSomethingElse();
        break;
}
Error prevention rules are essential for reducing production failures. Keeping them enabled is strongly recommended.

❓ Questions and Answers

Q1: What are the main differences from earlier ESLint versions?

ESLint has improved performance for rule configuration and plugin support, updated rules for the modern JavaScript ecosystem, and improved support for TypeScript and modern syntax.


🧪 Practice

Exercise 1

Write a function that takes an array of numbers and returns the sum of the squares of its even numbers. Check it with ESLint's no-unused-vars and eqeqeq rules, then fix any unused variables or uses of ==.

function sumEvenSquares(arr) {
    let sum = 0;
    for (let i = 0; i < arr.length; i++) {
        const num = arr[i];
        if (num % 2 == 0) {
            sum += num * num;
        }
    }
    return sum;
}

Exercise 2

Configure ESLint and Prettier to work together. Write a .eslintrc.js configuration that keeps formatting and syntax checks compatible, then write some code to test the semi and quotes rules.

const message = 'Hello, ESLint and Prettier!';
console.log(message);

🎉 You now understand common ESLint rules and how to use them! With a suitable configuration, you can write more consistent, maintainable frontend code and help your team protect code quality.