Closures & Callback in JavaScript

Closures:-

One of the greatest features in javascript is closures and it’s widely used but a little bit confusing term of javascript. So let’s understand what is closures. In simple words closures function are gives you access to an outer function from the inner function.

We will start the fundamental term lexical scope known as static scoping. Lexical scoping means is determined by the position of the variable which is declared in our nested scopes or function. Let’s see an example.

For Example:-

We can easily understand how to work lexical scope in our closures. But we talk about the closure function let’s see.

In other words, here is innerClosureFunc is a Closure because over the variable outerClosureFunc from its lexical scope.

Callback:-

What is a callback?

A callback is just a function that takes some time to give our results and generally async terms(async short for asynchronous) are used for accessing the value from our server. And callback concept is a great example of handling asynchronous behavior in javascript. Let’s see an example.

Output is

“I am calling…

Hello World”

Leave a Reply