0

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>

13
  • 2
    I do not get the same error? Please can you make sure the example is a minimal reproducible example? Commented Jul 14, 2021 at 9:32
  • 2
    Why is there an if around the function three? This does not really make sense. And be careful about the async context Commented Jul 14, 2021 at 9:34
  • 2
    calling a function on a condition makes sense but why the definition? Commented Jul 14, 2021 at 9:34
  • 4
    The three function only exits in the scope of the if statement, that's why you can't access it inside two. Commented Jul 14, 2021 at 9:35
  • I made you a snippet Commented Jul 14, 2021 at 9:35

3 Answers 3

1

As already mentioned in the comments, the function is defined in a block and thats why it is not working. You can use a proper IDE which highlights such issues.

Remove the if around the three() function:

See: https://jsfiddle.net/L3pz2mv4/

const elem = document.querySelector('.elem');

async function one() {
  two();
}
one();

async function two() {
  if (elem) {
    three();
  }
}

async function three() {
  console.log('works');
}
<div class="elem">
  test
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

You can use Stackoverflow code snippet instead. meta.stackoverflow.com/a/356679/14032355
No worries, just a reminder. It is not a big deal.
0

Here is example how you can make it work using function expression

const elem = document.querySelector('.elem');

var three;

async function one() {
  two();
}
one();

async function two() {
  if (elem) {
    three();
  }
}

if (elem) {
  three = function() {
    console.log('works');
  }
}

now function three will work outside if statement

Comments

0

two changes:

  • call one() after three has been defined
  • declare three at the top level, and then redefine it in the condition so that it is accessible everywhere
const elem = true;
let three;

async function one() {
  two();
}


async function two() {
  if (elem) {
    three();
  }
}

if (elem) {
  three = async function() {
    console.log('works');
  }
}

one();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.