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:
Is it the language design flaw?
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:
Is it the language design flaw?
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.
k
is a variable. You're instead passing the string'k'
to the for loop, and that doesn't work, and naturally the for loop never iterates if there are no keys, without throwing errors as that would cause a ton of issues, while just usingin
to look for keys in anything that isn't an object fails, with errors, as it should.typeof null
= "object", so technically it is looking for keys in an object.typeof
return is a language design oversight, not its behavior.