15

I coded the following:

showTitles = (typeof showTitles !== 'undefined') ? showTitles : 'Y';
showSelectGroup = (typeof showSelectGroup !== 'undefined') ? showSelectGroup : 'Y';

But JSLint is saying:

Warning 3 JS Lint: Unexpected 'typeof'. Use '===' to compare directly with undefined.

How should I change my code?

Matthew Simoneau
  • 6,199
  • 6
  • 35
  • 46

3 Answers3

9

Probably by using

showTitles = (showTitles === undefined) ? 'Y' : showTitles;
showSelectGroup = (showSelectGroup === undefined) ? 'Y' : showSelectGroup;

jslint has no issues with that (assuming showTitles and showSelectGroup are declared with var)

However, I'd write it as

var showTitles = showTitles || 'Y';
var showSelectGroup = showSelectGroup || 'Y';
CaffGeek
  • 21,856
  • 17
  • 100
  • 184
8

Note that whether this is best practice in general is debatable, but if you want to make it work with JSLint, you could do this

showTitles = (showTitles !== undefined) ? showTitles : 'Y';
Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
  • 1
    @Marilou not if you're using strict comparison. That being said, I would encourage doing research on that and deciding whether you want to do stuff that way for yourself and not just blindly follow JSLint's "advice". – Alex Turpin Sep 27 '12 at 18:50
  • Have you initialized your variable beforehand? [This code](http://jsfiddle.net/ASfgD/1/) passes JSLint and doesn't have any errors. – Alex Turpin Apr 09 '13 at 14:30
  • 10
    But if you are trying to test whether or not a variable is defined, you have to use typeof. Yet JSLint still complains about it. Very annoying. – mattacular May 29 '13 at 15:59
  • @mattacular Do you have a use case for which comparing to `undefined` does not work properly? – Alex Turpin May 29 '13 at 19:04
  • 1
    Yeah... when you are trying to test whether or not a variable is defined. Most of the time, you can refactor your code so that it isn't necessary (someVariable || false) but there are edge cases where you need to test for that and a typeof is the only way I'm aware of to do it safely. For example "aVariableThatMightBeUndefined === undefined" will throw a ReferenceError – mattacular May 31 '13 at 13:56
  • 3
    @mattacular you can simply use context for that. `this.aVariableThatMightBeUndefined === undefined`. – Alex Turpin Jun 03 '13 at 20:54
  • 7
    If you need to test of the existence of a global var you must use `typeof`. – Rico Sonntag Jun 21 '13 at 07:32
  • @RicoSonntag no such thing as a "global var" in JavaScript. They all have context you can use. – Alex Turpin Jun 21 '13 at 18:45
  • 2
    @Alex Turpin so would the correct way be window['variableWhichImTesting'] === undefined – szydan Apr 11 '14 at 15:02
  • @szydan yep, you got it! – mattacular May 12 '14 at 16:04
8

This message reflects the latest best practices. As of ES5 strict mode, the global value of undefined can no longer be changed and a direct comparison is simpler code and faster. In short, JSLint is aware of all this, and is giving you good advice.

In this case, change typeof showTitles !== 'undefined' to showTitles === undefined.

Matthew Simoneau
  • 6,199
  • 6
  • 35
  • 46