I have 3 functions to simplify question i wrote only code structure without entire code I'm checking if element is defined using if statement and if elemet exists then function three must be executed but code is not working.
function three must be defined only then if element exists but it seems i can't define function inside if statement
error: Uncaught (in promise) ReferenceError: three is not defined
Could you explain what is wrong and why i get undefined error?
const elem = document.querySelector('.elem');
async function one() {
two();
}
one();
async function two() {
if (elem) {
three();
}
}
if (elem) {
async function three() {
console.log('works');
}
}
<div class="elem">Elem</div>