How to check if a variable exists in JavaScript (defined/initialized)?
Hello everyone! I just started learning JavaScript and encountered a problem. I have code where I'm trying to use a variable, but it may not be defined. I want to check if a variable exists (i.e., has been declared and initialized) before using it.
For example, I have this code:
```javascript
var myVar;
console.log(myVar); // undefined
console.log(typeof myVar); // "undefined"
if (myVar) {
console.log("Variable exists!");
}
```
But here I understand that `myVar` is declared but not initialized, so `typeof` shows `"undefined"`. What if the variable is not declared at all?
```javascript
if (unknownVar) {
console.log("This variable does not exist!");
}
```
This code throws an error `ReferenceError: unknownVar is no