About
In this code snippet, we’ll learn about callbacks in Javascript.
Callbacks are nothing more than function pointers passed as an argument into another function to be then called from within that function.
This example might seem a bit useless but the concept of callbacks becomes much more useful when we start dealing with asynchronous behavior. Like for example when making an API call, events, timers, …
Code:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
isNumber(5, logToConsole);
function logToConsole(text){
console.log(text);
}
function isNumber(input, callback){
if(typeof input == "number")
callback("It's a number.");
else
callback("Not a number.");
}
isNumber(5, logToConsole);
function logToConsole(text){
console.log(text);
}
function isNumber(input, callback){
if(typeof input == "number")
callback("It's a number.");
else
callback("Not a number.");
}
isNumber(5, logToConsole); function logToConsole(text){ console.log(text); } function isNumber(input, callback){ if(typeof input == "number") callback("It's a number."); else callback("Not a number."); }