-1

I found this example on w3schools about js callbacks. It's supposed to output "Goodbye". My question is why isn't "hello" printed first since myFirst() is the first function call followed by mySecond? The code snippet is attached below.

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

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

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

myFirst();
mySecond();
<!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>

</body>
</html>

4
  • It is. But because you're overwriting the HTML each time you call that function you won't see it. Try innerHTML += some instead. Commented Apr 20, 2022 at 17:34
  • Your functions are called one after the other with no pause in between. The browser will only show the net result after all the changes to the DOM are complete. Commented Apr 20, 2022 at 17:34
  • 1
    Thanks to both of you. That clears things up. Commented Apr 20, 2022 at 17:36
  • And, FWIW, those are just plain old function calls. "Callbacks" are a specialized implementation. Commented Apr 20, 2022 at 17:43

1 Answer 1

1

the function myfirst is working first then the second function ovewrite it at the same time so you just see goodbye not hello!

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.