When to Use "variable === undefined" vs. "typeof variable === 'undefined'"
According to the jQuery Core Style Guidelines, there are two methods to determine whether a variable is defined:
Why the Distinction?
The distinction between these approaches stems from the fundamental difference between declared and undeclared variables in JavaScript.
When a variable is declared but not assigned a value, its value is considered to be undefined. Checking if the variable is undefined using variable === undefined would return true for such variables.
However, if a variable is not declared at all (undeclared variable), checking if it is undefined using variable === undefined would trigger a ReferenceError with the message "'variable is not defined'".
Specific Use Cases
By using these conventions, jQuery ensures that variable checks are performed consistently and without unexpected errors, based on the variable's scope and declaration status.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3