Yup, typeof
is the safest play. But if you’re already sure the variable’s declared in your scope, you can clean it up a bit. My go-to javascript check if undefined in that case:
if (myVariable === undefined) {
// yep, it's undefined
}
It’s more readable and intentional, especially when you’re debugging or writing code in a well-defined block. Just remember, it’ll break if myVariable
was never declared, so use it with scoped confidence.