Site icon ni18 Blog

JavaScript and Linters: ESLint and JSHint

Hey, budding coder! If you’re getting into JavaScript (JS)—the language that makes websites dance—you’ve probably noticed it’s super flexible. You can write code fast, but that freedom can also sneak in some sneaky bugs or sloppy habits. Ever had a program crash because of a missing semicolon or a typo you didn’t spot? Yeah, we’ve all been there! That’s where linters like ESLint and JSHint swoop in to save the day. These tools are like your personal code coaches—they check your work before it runs, catching mistakes and nudging you toward cleaner, pro-level code. In this guide, I’ll break down what linters are, why they’re your new BFFs, and how to use ESLint and JSHint like a champ in 2025. Ready to make your JS rock-solid? Let’s dive in!


What’s a Linter, and Why Should You Care?

Picture this: You’re writing a killer JS app, feeling like a genius, and then—bam—it crashes because of a tiny error you didn’t see. A linter is a tool that scans your code without running it and flags problems like bugs, weird syntax, or stuff that’s just bad practice. It’s like having a spellchecker for your code, but way smarter.

The name “linter” comes from an old Unix tool that checked C code back in the day—think of it as a lint roller picking up fluff from your sweater, but for programming. In JavaScript’s wild, dynamic world (no strict types here!), linters are a must to keep chaos in check.

Why Use a Linter?

For a newbie in 2025, linters are your safety net as you learn JavaScript’s quirks. Let’s meet the two big players: ESLint and JSHint!


ESLint: Your Customizable Code Cop

What Is ESLint?

ESLint is an open-source linter launched by Nicholas C. Zakas in 2013, and it’s the go-to for modern JS devs. It’s like a super-smart assistant that analyzes your code, spots issues, and lets you tweak its rules to fit your style. Whether you’re coding solo or on a team, ESLint’s got your back.

How Does ESLint Work?

It’s a two-step ninja move:

  1. Parsing: ESLint turns your code into an Abstract Syntax Tree (AST)—a fancy map of your program’s structure. Think of it like breaking your code into Lego blocks it can study.
  2. Traversing: It walks through that AST, checking for patterns that break your rules—like missing semicolons or funky variable names. Each “rule” is a plugin, so it’s mega-customizable.

Getting Started with ESLint

Ready to try it? Here’s how to set it up:

Step 1: Install ESLint

You’ll need Node.js (it runs JS outside the browser—grab it from nodejs.org if you haven’t). Then:

  npm install eslint --global

(Or use npx later if you skip global install.)

Step 2: Set Up a Project

  mkdir my-js-project
  cd my-js-project
  let name = "Alex"
  console.log(name)

Step 3: Configure ESLint

  npx eslint --init
  {
    "env": {
      "node": true,
      "es2021": true
    },
    "rules": {
      "semi": ["error", "always"],
      "quotes": ["error", "double"]
    }
  }

Step 4: Run ESLint

  npx eslint app.js
  1:5  error  Missing semicolon  semi
  1:10 error  Strings must use doublequote  quotes
  let name = "Alex";
  console.log(name);

Real-World Example

Check this messy code:

var x = 5
function sayHi() {
  console.log('Hi ' + x)
}
sayHi()

ESLint with rules like "no-var": "error" and "semi": "always" will flag:

Fixed:

const x = 5;
function sayHi() {
  console.log("Hi " + x);
}
sayHi();

Why ESLint Rocks


JSHint: The Flexible Classic

What Is JSHint?

JSHint is an older, community-driven linter that’s been around since 2011 (forked from JSLint). It’s simpler than ESLint but still packs a punch for spotting errors and enforcing rules. It’s less customizable but great for quick setups or smaller projects.

How Does JSHint Work?

Similar to ESLint:

  1. Parsing: Turns your code into an AST.
  2. Traversing: Checks it against its rules—less granular than ESLint, but effective.

Getting Started with JSHint

Let’s set it up:

Step 1: Install JSHint

  npm install jshint --global

Step 2: Create a File

  let name = "Alex"
  console.log(name)

Step 3: Configure JSHint

  {
    "curly": true,
    "undef": true,
    "node": true
  }

Step 4: Run JSHint

  npx jshint app.js
  app.js: line 1, col 11, Missing semicolon.
  app.js: line 2, col 15, Missing semicolon.
  let name = "Alex";
  console.log(name);

Real-World Example

Try this:

x = 10
if (x > 5)
  console.log("Big")

JSHint flags:

let x = 10;
if (x > 5) {
  console.log("Big");
}

Why JSHint Rocks


ESLint vs. JSHint: Which One’s for You?

FeatureESLintJSHint
CustomizabilitySuper high—tons of rulesDecent but limited
PluginsYes—React, TS, moreBasic, fewer add-ons
Ease of UseSteeper curveBeginner-friendly
FixingAuto-fix with --fixManual fixes
CommunityHuge, active (2025)Smaller, steady

Practical Example: Lint a Mini-App

Here’s a messy JS snippet:

var count = 0
function add(num) {
  count = count + num
  console.log('Total: ' + count)
}
add(5)

Run it through both:


Pro Tips for Linting in 2025


At End: Lint Your Way to Better JS

ESLint and JSHint are your JavaScript superheroes—catching bugs, enforcing style, and teaching you the ropes before runtime disasters strike. In 2025, they’re must-haves for any coder, from solo hobbyists to team devs. ESLint’s your customizable powerhouse; JSHint’s your quick-and-easy pal. Set them up, play with the rules, and watch your code quality soar. So, grab your terminal, lint some JS, and feel the pro vibes. Which linter will you try first? Let me know—I’m stoked to hear about your wins!

Exit mobile version