Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the JSHint option "undef" do?

From reading the documentation the setting undef seems to be what controls the "x is not defined" warnings. But setting it to false does not stop those warnings. So this leaves me wondering what undef actually does.

Could someone please explain this better than the documentation does?

Note: To ignore these warnings I had to use /*jshint -W117 */

like image 578
Hal Carleton Avatar asked Feb 23 '26 13:02

Hal Carleton


1 Answers

The undef option, when enabled, warns whenever non-local (neither a parameter nor "var" variable in scope) variable usage is found.

Taking the code in the example, the following results in 'myvar' is not defined. (This warning indicates that the code may result in a ReferenceError when the code is run; not that the value is "undefined".)

/*jshint undef:true */
function test() {
  var myVar = 'Hello, World';
  console.log(myvar); // Oops, typoed here. JSHint with undef will complain
}

When disabling the option there is no warning as it's assumed that the intent was to access the myvar gobal variable; this can in turn be accepted/validated with the global directive, and the following is once again warning-free.

/*jshint undef:true */
/*global myvar*/
function test() {
  var myVar = 'Hello, World';
  console.log(myvar); // Yup, we wanted the global anyway!
}
like image 184
user2864740 Avatar answered Feb 26 '26 03:02

user2864740