Knowing these concepts can help you stand out during interviews
But the journey of this language was not so smooth. This language was designed by
Brendan Eich and first appeared on December 4, 1995. At the initial moment, it was only used to bring interactivity to the web pages while the HTML and CSS are used to structure and design the web page.
The Game-changing moment happens when
We will cover the following topics in this tutorial:
- Function as First-Class Citizen
- A function can be assigned as a value to a variable
- A function can be passed as an argument to other functions
- A function can be returned by another function
Here, you can see that we have declared an arrow function and later on, we have stored it into a variable named
Instead of hard-coding “Hello” in our function
myFunc, we now accept a parameter which is a function. As you can see, there are three different functions
salut and each of them are passed as an argument to
We have a function named
outerFunc. It doesn’t do much, it simply prints a statement to the console. Another function called
innerFunc is created inside. The
innerFunc also doesn’t do much, it simply prints a statement to the console. The
outerFunc returns the
There are a couple of ways this can be used. The first way is to use two braces together. The first pair of brackets invokes
outerFunc and the second pair of brackets invokes the object returned by
outerFuncie the function
In the second way, we store the returned function in a variable and invoke it it like a normal function
In simple terms, Currying is a technique of chaining multiple functions. It’s a functional programming feature. Let’s understand this concept by solving a common interview question:
Look at the below code snippet.
The task you need to perform is to call the sum function like sum(10)(20)(30) instead of sum(10, 20, 30) this.
// Current way of Invoking function
sum(10, 20, 30)// Desired way of Invoking function
How can you perform this action? The answer is simple, the term you need to use is Currying. See the below code example
Here, you can see that we are getting the output as expected. But the main question is how we are getting this? Let’s break it down in the below section:
At first, we declared a function named
sayHello() and stored a string inside the
name variable. Later on, we declared another function and print the name with a
Hello message, and returned the
inner() function. Finally, we have stored the returned function into the
name variable as the
sayHello() function’s memory has been destroyed. But how we are getting the output as expected?
The main reason behind this is that Closure. Here, it is true that the
sayHello() function’s memory has been destroyed but it creates a reference of that function and as a result, while executing the
inner() it can access the variable from the reference of the
A callback is a function passed as an argument to another function. Let’s look at a use case below.
Suppose, we have an array of names saved in our database. We want to add a new name and later on, we want to print the names with the newly added name. To perform this action we have written the below code:
Here, you can see that, we have added the functionality for adding new names but in the output, the new name has not been added. The main reason behind this is the
We need to use a callback function so that we can break the
We have passed a function (printNames) as an argument to another function (addNewName). This time
printNames will execute only after the previous functions have been executed. This concept is known as the Callback and you can see that we have called this function after the previous function finished its tasks. That’s the reason we have mentioned Callback as Call later.
There are many advanced concepts that have been built on top of the Callback. You may also have heard about the concept of
Callback hell. I’ll be discussing this in one of the future articles.