Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why if(key in null); throw exception while for(key in null); does not, it is the language design flaws on it?

From a language design perspective, why:

if('k' in null);

TypeError: Cannot use 'in' operator to search for 'k' in null

BUT:

for('k' in null);

prints undefined

in ECMAScript spec:

  • 11.8.7 The in operator
  • 12.6.4 The for-in Statement

Is it the language design flaw?

like image 558
justjavac Avatar asked Dec 31 '13 08:12

justjavac


1 Answers

From a design perspective, it's hard to say what the appropriate return value of k in null should be (true is clearly wrong, but false is misleading), but it's easy to say that in the for-in statement, you should just skip the loop.

I don't agree with this decision at all - I think that for (k in null) should throw an error, especially if running in strict mode. But you can see how the difference would arise.

like image 86
Chris Hayes Avatar answered Oct 13 '22 02:10

Chris Hayes