Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

433
Views
Javascript function when called twice does not execute

I am trying to understand a tutorial about function sequencing on 'w3schools - Lesson: JS Async', but i am stuck at the below example.

If you have a JS function that is called twice, apparently the order in which the function is called determines which function gets executed, in the example below i would expect the first function to be executed but it's not

The essence of this lesson should be that the declaration of a function does not determine it's execution order, only the way it's called, but i can't grasp it..

Can anyone elaborate or explain the logic of this syntax? Or what is happening under the hood in JS?

Link to tutorial: https://www.w3schools.com/js/js_callback.asp

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Function Sequence</h2>

<p>JavaScript functions are executed in the sequence they are called.</p>

<p id="demo"></p>

<script>
function myDisplayer(some) {
  document.getElementById("demo").innerHTML = some;
}

function myFirst() {
  myDisplayer("Hello");
}

function mySecond() {
  myDisplayer("Goodbye");
}

myFirst();
mySecond();
</script>

</body>
</html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can verify that both functions are called in the correct order by including a console.log statement in the myDisplayer function.

Because you're overwriting the entire content of #demo each time you call the function, you'll only get to see the result of the very last function call.

function myDisplayer(some) {
  console.log(some);
  document.getElementById("demo").innerHTML = some;
}

function myFirst() {
  myDisplayer("Hello");
}

function mySecond() {
  myDisplayer("Goodbye");
}

myFirst();
mySecond();
<p id="demo"></p>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!