Why if (key in null); throw an exception while for (key at zero); Is this not a language design error?

In terms of language design, why:

if('k' in null);

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

BUT:

for('k' in null);

prints undefined

in the ECMAScript specification:

Is this a language design error?

+7
javascript ecmascript-5
source share
1 answer

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

I do not agree with this decision at all - I think that for (k in null) should throw an error, especially if it works in strict mode. But you can see how the difference arises.

+2
source share

All Articles