JavaScript Callbacks
JavaScript Callbacks
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT
JavaScript Callbacks
❮ Previous Next ❯
Function Sequence
JavaScript functions are executed in the sequence they are called. Not in the sequence they are defined.
Example
function myFirst() {
myDisplayer("Hello");
}
function mySecond() {
myDisplayer("Goodbye");
}
myFirst();
mySecond();
Try it Yourself »
Example
function myFirst() {
myDisplayer("Hello");
}
function mySecond() {
myDisplayer("Goodbye");
}
mySecond();
myFirst();
Tutorials Exercises Services Spaces Get Certified My W3Schools
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT
Try it Yourself »
Sequence Control
Sometimes you would like to have better control over when to execute a function.
You could call a calculator function (), save the result, and then call another function () to display the
result: myCalculator myDisplayer
Example
function myDisplayer(some) {
document.getElementById("demo").innerHTML = some;
}
Try it Yourself »
Or, you could call a calculator function (), and let the calculator function call the display function
(): myCalculator myDisplayer
Example
function myDisplayer(some) {
document.getElementById("demo").innerHTML = some;
}
myCalculator(5, 5);
Try it Yourself »
The problem with the first example above, is that you have to call two functions to display the result.
The problem with the second example, is that you cannot prevent the calculator function from displaying the result.
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT
Using a callback, you could call the calculator function () with a callback (), and let the calculator function run the callback
after the calculation is finished: myCalculator myCallback
Example
function myDisplayer(some) {
document.getElementById("demo").innerHTML = some;
}
myCalculator(5, 5, myDisplayer);
Try it Yourself »
Note
When you pass a function as an argument, remember not to use parenthesis.
Example
// Create an Array
const myNumbers = [4, 1, -20, -7, 5, 9, -6];
// Display Result
document.getElementById("demo").innerHTML = posNumbers;
Try itCSS
HTML YourselfJAVASCRIPT
» SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT
Where callbacks really shine are in asynchronous functions, where one function has to wait for another function (like waiting
for a file to load).
❮ Previous Next ❯
CONTACT US
W3.CSS Tutorial
Tutorials Exercises
Bootstrap Tutorial
Services Bootstrap Reference
PHP Reference
Spaces W3.CSS Examples
Get Certified
Bootstrap Examples
My W3Schools
PHP Tutorial HTML Colors PHP Examples
HTML
CSS JavaJAVASCRIPT
Tutorial SQL PYTHON Java Reference PHP
JAVA HOW TO W3.CSS JavaCExamples
C++ C# BOOTSTRAP REACT
C++ Tutorial Angular Reference XML Examples
jQuery Tutorial jQuery Reference jQuery Examples
Get Certified
HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.