Javascript Callbacks

Code Snippets Callbacks
Share:

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 calleventstimers, …

Code:

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.");
}

Resulting output:

Share:

Leave a Reply

Your email address will not be published.

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger