This already has an answer on Stack Overflow. It's using typeof
operator.
As such:
Chrome developer console screenshot
In this tests, there's an undeclared variable called rey
The recommended method
On JSLint, this method throws error
message.
This message:
Unexpected 'typeof'. Use '===' to compare directly with undefined
.
To fix that, simply declare the gonna-be-initialized-later variable as a var
. Then compare that variable directly with the undefined
property using triple equals (===
). Or using Boolean negation.
Like so:
On Chrome console:
Links
- Discussion on Stack Overflow.
- The
JavaScript
quality tool: JSLint. - The
undefined
primitive on MDN.
No comments:
Post a Comment
Tell me what you think...