JSHint - JavaScript Static Analysis Tool

2.13.6 · maintenance · verified Sun Apr 19

JSHint is a static code analysis tool for JavaScript that identifies errors and potential problems, including typos, syntax errors, implicit type conversions, and variable leaks. Its primary goal is to help developers prevent common mistakes in complex JavaScript programs. The current stable version is 2.13.6, with its most recent update in November 2022. While it previously saw regular bug-fix releases, its development cadence has slowed considerably compared to its peak. A key differentiator for JSHint is its focus on detecting bugs and structural issues in the code rather than primarily enforcing code style, offering high configurability through `.jshintrc` files and inline comments to adapt to diverse execution environments. It predates many modern linters and has historically been crucial for spotting critical 'red flags' that could lead to extensive debugging if left unaddressed.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates programmatic usage of JSHint to lint a JavaScript string, configured for a Node.js environment, and prints any detected errors or warnings.

const { JSHINT } = require('jshint');

const javascriptCode = `
function greet(name) {
    console.log('Hello, ' + name)
    // Missing semicolon here intentionally
}

const myName = 'World';
greet(myName);

// A variable not defined in this scope
undefinedVar = 1;
`;

// JSHint options: enforce strict mode, allow console
const options = {
    strict: true,
    browser: false, // For Node.js environment
    node: true,     // For Node.js globals like console
    undef: true     // Warn if variables are used without being declared
};

// Lint the code
JSHINT(javascriptCode, options);

// Log any errors found
if (JSHINT.errors.length > 0) {
    console.log('JSHint found issues:');
    JSHINT.errors.forEach(error => {
        if (error) {
            console.error(`  Line ${error.line}, Col ${error.character}: ${error.reason} (${error.code})`);
        }
    });
} else {
    console.log('No JSHint issues found.');
}

view raw JSON →