Skip to main content

JavaScript Callbacks

JavaScript Callbacks
Hi guys. Here we are again with another exciting JavaScript lesson. So far you have been learning about other JS concepts which are of great importance as you proceed with your studies. In this lesson, we shall be looking at a different notion known as JavaScript Callbacks. Sounds interesting right? Put on your study attire and get set. It is going to be an exciting moment learning something new. 

JavaScript Callbacks, also known as callback functions are functions passed into another one as an argument. This technique normally allows a function to call another function. The callback functions can run after another function has finished depending on how they are called. This is because JavaScript functions are normally executed in the order in which they are being called and not in the sequence they are defined. Let us take an example to illustrate the concept of callbacks.

We need to first of all define the function we are going to be using as our callback function. Consider the lines of code below:

function displayMessage(message){
alert(message);
}

This function alerts any message sent to it as an argument. Let’s take for example; I you pass in the string “Hello World”, it is going to alert the message. Let us see how JavaScript does it.

JavaScript Callbacks

You see the output right? Clean and clear as we earlier expected. Let us proceed in demonstrating the concept of callbacks. We need to define another function and pass this our first function as an argument to our second function. Consider the lines of code below.

function myMessage(){
displayMessage(“I am coding in JavaScript.”);
}

The above lines of code makes use of the function “displayMessage( )”. Hence, the function “displayMessage( )” is our callback function since it is being called to action by another function. Let us see how these all play out:

JavaScript Callbacks

Wow! Take a look at how the code works beautifully. This is so perfect and exciting. 

Let us play around with it by using different functions to implement this concept. Consider the following lines of code:

function displayAnswer(ans){
alert(ans);
}
function add(a,b){
sum = a+b;
displayAnswer(sum)
}
add(4,7);

Let us see if our code works…

JavaScript Callbacks

There you have it. It works.

Now we have come to the end of this lesson. Hope you learnt a lot while going through it? For more lessons, stay tuned, and also, don’t forget to subscribe to our YouTube channel. We are here to provide the best. Wishing you the best, and less bugs as you code.


Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...