Javascript

JSHint and jQuery is not defined

27 September 2026 · 8 min read

JSHint and jQuery  is not defined

Writing clean and maintainable JavaScript code is crucial for any web development project, especially when working with libraries like jQuery. Ensuring code quality from the outset saves significant time and resources down the line. That’s where tools like JSHint come into play. JSHint is a static code analysis tool that helps developers identify potential errors, enforce coding styles, and improve overall code quality. Integrating JSHint with jQuery projects can dramatically reduce bugs and promote better coding practices, leading to more robust and efficient web applications. This article will guide you through effectively using JSHint with your jQuery code to enhance the reliability and readability of your projects.

Understanding JSHint and its Benefits

JSHint is more than just a linter; it’s a valuable ally in your development process. It analyzes your JavaScript code without executing it, identifying potential problems such as syntax errors, undeclared variables, and violations of established coding conventions. By highlighting these issues early, JSHint helps you avoid runtime errors and ensures your code adheres to a consistent style. This is especially important in collaborative environments where multiple developers are working on the same project. According to a study by Coverity, static analysis tools like JSHint can reduce defect density by up to 15% Synopsys.

One of the primary benefits of using JSHint is its configurability. You can customize JSHint’s rules to match your team’s specific coding style and project requirements. This flexibility allows you to enforce best practices without being overly restrictive. For instance, you can configure JSHint to warn about the use of global variables, enforce strict mode, or require consistent indentation. By tailoring JSHint to your needs, you can ensure that your code is not only error-free but also conforms to your team’s coding standards. This leads to more readable, maintainable, and collaborative codebases. Another advantage of using JSHint is that, it can be integrated into your development workflow using various plugins for text editors and IDEs.

Furthermore, JSHint promotes code clarity and reduces the likelihood of introducing bugs during refactoring. By identifying potential issues early, you can address them before they become more complex and harder to fix. This proactive approach saves time and effort in the long run and improves the overall quality of your code. Investing in code quality tools like JSHint is an investment in the long-term maintainability and success of your web development projects. It also helps developers better understand the nuances of Javascript and common pitfalls.

Setting Up JSHint for your jQuery Projects

Integrating JSHint into your jQuery projects is a straightforward process. The first step is to install JSHint. You can install it globally using npm (Node Package Manager) with the command: npm install -g jshint. This allows you to run JSHint from the command line. Alternatively, you can install it as a project dependency using npm install jshint --save-dev, which is useful for ensuring that everyone working on the project uses the same version of JSHint. Once installed, you can configure JSHint using a .jshintrc file in your project’s root directory. This file specifies the rules and options that JSHint should use when analyzing your code.

The .jshintrc file is a JSON file that allows you to customize JSHint’s behavior. You can specify which rules to enforce, which warnings to ignore, and which global variables to allow. For example, if you are using jQuery, you can add "jquery": true to the .jshintrc file to tell JSHint that the $ variable is a valid global variable. Here’s an example .jshintrc file:

{ "esversion": 6, "jquery": true, "globals": { "$": true, "console": true }, "strict": false, "unused": true } 

This configuration specifies that the code should be interpreted as ECMAScript version 6, that jQuery is being used, that the $ and console variables are global, that strict mode is not enforced, and that unused variables should be reported. You can find a comprehensive list of available options and rules in the JSHint documentation JSHint Docs. Once you have configured JSHint, you can run it from the command line by navigating to your project directory and running jshint yourfile.js. You can also integrate JSHint into your build process using tools like Grunt or Gulp to automatically check your code whenever you make changes. Internal link example

Best Practices for Using JSHint with jQuery

To maximize the benefits of using JSHint with jQuery, it’s essential to follow some best practices. First, be consistent with your coding style. JSHint can help you enforce a consistent style, but it’s up to you to define what that style is. Use consistent indentation, naming conventions, and code formatting. This will make your code more readable and easier to maintain. Second, address JSHint warnings promptly. Don’t ignore warnings or treat them as insignificant. Each warning represents a potential problem that should be investigated and addressed. Ignoring warnings can lead to more serious errors down the line.

Here are some key points to consider:

  • Always define variables before using them. JSHint will warn you about undeclared variables.
  • Avoid using global variables. Global variables can lead to naming conflicts and make your code harder to reason about.
  • Use strict mode. Strict mode helps you write more robust and secure code by preventing certain common mistakes.

Another best practice is to use JSHint in conjunction with other code quality tools. JSHint is a great tool for identifying potential errors and enforcing coding styles, but it’s not a substitute for thorough testing. Use unit tests and integration tests to verify that your code is working correctly. Additionally, consider using a code review process to get feedback from other developers. Code reviews can help you identify potential problems that JSHint might miss. According to a study by SmartBear, code reviews can reduce defect density by up to 20% SmartBear.

Here’s an ordered list of steps to effectively integrate JSHint:

  1. Install JSHint globally or as a project dependency.
  2. Create a .jshintrc file in your project’s root directory.
  3. Configure the .jshintrc file with your desired rules and options.
  4. Run JSHint from the command line or integrate it into your build process.
  5. Address JSHint warnings promptly.

Advanced JSHint Configuration and Usage

For more advanced usage, JSHint offers a wide range of configuration options that allow you to fine-tune its behavior. You can use the "predef" option to define global variables that are not part of the standard JavaScript environment, such as variables defined by your framework or library. You can also use the "globals" option to explicitly declare global variables and specify whether they are read-only or read-write. This can help you avoid accidental modifications to global variables.

Consider these key aspects:

  • Use the "esversion" option to specify the ECMAScript version that your code is using. This ensures that JSHint correctly parses your code and identifies any syntax errors.
  • Use the "bitwise" option to warn about the use of bitwise operators. Bitwise operators are often used incorrectly, so it’s a good idea to enable this option to catch potential mistakes.

Furthermore, you can integrate JSHint with your text editor or IDE to get real-time feedback as you type. Many popular editors, such as Visual Studio Code, Sublime Text, and Atom, have plugins that automatically run JSHint and display warnings in the editor. This allows you to catch errors and enforce coding styles as you write code, making the development process more efficient. For example, the ESLint extension for VS Code provides real-time linting and code analysis, helping you identify and fix issues before they become more serious. By leveraging these advanced configuration options and integrations, you can maximize the benefits of using JSHint and ensure that your code is of the highest quality.

Infographic here
FAQ About JSHint and jQuery ---------------------------
What is JSHint?
JSHint is a static code analysis tool for JavaScript that helps identify potential errors, enforce coding styles, and improve code quality.
Why should I use JSHint with jQuery?
Using JSHint with jQuery helps you write cleaner, more maintainable code, reducing bugs and improving the overall reliability of your jQuery projects.
How do I install JSHint?
You can install JSHint globally using npm with the command `npm install -g jshint` or as a project dependency with `npm install jshint --save-dev`.
How do I configure JSHint?
You can configure JSHint using a `.jshintrc` file in your project's root directory, specifying the rules and options that JSHint should use.
By integrating JSHint into your workflow, you're proactively investing in the stability and maintainability of your code. While initially, it might seem like an extra step, the long-term benefits of reduced bugs, improved code clarity, and consistent styling far outweigh the initial effort. Embrace JSHint as a partner in your development journey, and watch your jQuery projects flourish with quality and reliability. Why not start today by adding JSHint to your next project and experiencing the difference firsthand? Consider exploring other code quality tools and techniques to further enhance your development practices.

Question & Answer :
The following JS:

(function() { "use strict"; $("#target").click(function(){ console.log("clicked"); }); }()); 

Yields:

test.js: line 5, col 3, '$' is not defined. 

When linted using JSHint 0.5.5. Any ideas?

If you are using a relatively recent version of JSHint, the generally preferred approach is to create a .jshintrc file in the root of your project, and put this config in it:

{ "globals": { "$": false } } 

This declares to JSHint that $ is a global variable, and the false indicates that it should not be overridden.

The .jshintrc file was not supported in really old versions of JSHint (such as v0.5.5 like the original question in 2012). If you cannot or do not want to use the .jshintrc file, you can add this at the top of the script file:

/*globals $:false */ 

There is also a shorthand “jquery” jshint option as seen on the JSHint options page..