最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jshint, eqnull, Use '===' to compare with 'undefined' - Stack Overflow

programmeradmin1浏览0评论

I apologize ahead of time, I am clearly utterly incapable of understanding how to configure jshint.

I have this line of code

if ( data == undefined || (typeof data === 'object' && data.length === 0) ) {
...

jshint is underlining the first == and saying

Use '===' to pare with 'undefined'.

I added /* jshint eqnull:true */. I thought this is what the option does. I even see an example here (search for eqnull).

What am I missing?

I apologize ahead of time, I am clearly utterly incapable of understanding how to configure jshint.

I have this line of code

if ( data == undefined || (typeof data === 'object' && data.length === 0) ) {
...

jshint is underlining the first == and saying

Use '===' to pare with 'undefined'.

I added /* jshint eqnull:true */. I thought this is what the option does. I even see an example here (search for eqnull).

What am I missing?

Share Improve this question edited Nov 20, 2015 at 21:26 Jonatas Walker 14.2k6 gold badges57 silver badges82 bronze badges asked Sep 4, 2014 at 1:40 Sam SelikoffSam Selikoff 12.7k13 gold badges60 silver badges107 bronze badges 5
  • eqnull suppresses the warning about paring with null, not undefined. – Barmar Commented Sep 4, 2014 at 1:49
  • Is it possible to suppress the warning about undefined? – Sam Selikoff Commented Sep 4, 2014 at 1:50
  • I don't see any mention of such a thing in the documentation. – Barmar Commented Sep 4, 2014 at 1:50
  • If you use " typeof data == 'undefined' " instead of " data == undefined ", no warning message will be shown. You don't need to suppress warning when no warning is displayed. – Fumu 7 Commented Sep 4, 2014 at 2:09
  • I'd like to use == undefined to coerce null to undefined. typeof null is object. – Sam Selikoff Commented Sep 4, 2014 at 2:19
Add a ment  | 

2 Answers 2

Reset to default 6

Use this option for the same

"-W041": false

In Javascript, undefined and null are two distinct values. Directly paring other values to undefined is problematic, because null == undefined, but typeof null != typeof undefined.

From your ment above, it seems that you explicitly want to check for both null and undefined values, in which case you could write (data === undefined || data === null).

If you are keen to keep a shorter expression, you could just write data == null, which is functionally identical. As you mention above, you will need to provide the /* jshint eqnull:true */ option.

发布评论

评论列表(0)

  1. 暂无评论