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

ecmascript-5, javascript

Solution

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.

Problem

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?

Original source