JavaScript Lessons

Callbacks in Javascript

In this lesson will be callback in Javascript described. A callback is a function passed as an argument to another function. This technique allows a function to call another function.

We see that in the sayHello function we pass the name as a parameter and in the body of the sayHello function we write the text Hello and the variable name in the document. In the function named show, we place two variables in the parameter. The anyFunction variable will be used to substitute the function, and the anyName variable will then be used to substitute the name.

We place the name of the function with the argument in the body of the Show function, these names must be the same as the names in the argument of the Show function. So now we can easily change the name of the function and the name in the parameters of the Show function.

In this case, we pass the sayHello function and the name John as arguments of the Show function. When pass a function as an argument, remember not to use parenthesis.

Now we can try to launch the page. We see that the Show function displays a Hello greeting and the name John correctly.

Download source files