what is the best usage for the "typeof" JavaScript function?
if (typeof (myvar) == 'undefined') {
//or
if (typeof (myvar) == undefined) {
//or
if (typeof myvar == 'undefined') {
//or
if (typeof myvar == undefined) {
Thanks
typeof
is an operator, not a function, and returns a string; so do not use parentheses and do compare it to a string.
When you compare things, avoid type coercion unless you need it (i.e. use ===
not ==
).
if (typeof myvar === 'undefined') {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With