Understanding Javascript Functions in Depth
Published: 2023-11-17 05:08:37

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29gg8ac2t1phubj22iqk.png)

Functions in JS are first-class objects or also known as first-class citizens.

A First-Class Citizen in a programming language is an entity that can be dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as other variables in the programming language have. In **[Javascript developer](https://www.ficode.co.uk/technology/java-development
)**, Functions are just like objects. The only difference is functions are invocable but objects are not.

**Example:
**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n50406ze165yiqed92yk.png)

**Callback Functions:
**A callback function (B) is a function that is passed as a parameter to another function (A).

And the function (A) calls the parameterized function (B) at some particular state of its execution.

Now two questions are here,

**Question 1:** Why pass a function as a parameter, if we can just call the function B from inside another function A.

**Answer 1:** Because many times both the functions are not written at the same time, from the same developer, for example, setTimeout() function is already there in the javascript but the callback function we pass into it varies for every problem. But we don’t just go every time and change the internal structure of the setTimeout function.

Another example is the sort() function, we can pass a callback to it in order to tell how to sort an array of objects etc (which key to use to order the objects in the array).

**Question:2**  If the parameterized Function (B) is to be called at the end of the function (A) then why not call function B right after the calling of function (A), instead of passing function B as a parameter.

**Answer2:** Okay you sound logical, but what if the function A is an asynchronous function and also will be used to perform various tasks in different scenarios upon completing its execution. In this case, you will need to pass the callback function because of two reasons,

1. You do not know prior to defining the function (A) that what the function (A)’s the user wants to run on completing its execution so function (B) would not be the same every time. (dynamically changing the after effect of function A.)
2. You cannot call function (B) right after function (A)’s calling because function (A) is async.

**Functions as Objects
**
In javascript, functions can also have properties just like objects.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqm48rm7g41qmdcjxmka.png)

By adding a property to a function, we can preserve the previous output the function generated, and many other things. We can also easily implement memorization by using this.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ccwdwqsfmtwn9xsot3pa.png)

**3. Immediately invoked functional Expressions (IIFE)
**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqcafboss0z1nimas56u.png)

A function definition inside parentheses makes it immediately invokable, and we can invoke them by ().

In the above example, we are passing an argument Const to the function.

And the main purpose of IIFE is modularization.

The main goal of IIFE is to avoid polluting the Global Scope.

Polluting the global namespace can be problematic because there is a chance of overriding variables and functions declared in different files.

And you also have to keep track of every variable declared in the Global Scope.

Arrow Functions: also called ( ‘=>’ Fat arrow Operator)
Arrow functions are just the syntactic sugar from ES6, to make the code shorter and more succinct.

For example: to sort array we wrote


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qydv2bagilu02nloio39.png)

But using the arrow function we could write it in a much shorter way.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7sr4dhfrbq2w6l18rn9.png)

Remember when we use curly braces, we need to use the return statement, else as in above case if we don’t use curly braces then we don’t need and cannot use return statements.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nj1mliw88ekvl83l0na1.png)

**Example 2:
**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81yxfjzxr0kdrls1fbyx.png)

But using Arrow function we could write it in more succinct manner:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9r0x2tncv0hzw7o7oiss.png)

If you want to hire a Javascript developer, **[Ficode](https://www.ficode.co.uk/)** is here to help you so please don’t hesitate to contact us today on [info@ficode.com](info@ficode.com).

## This post originally appeared on [Ficode](https://www.ficode.co.uk/) website, and we republished with permission from the author. Read the full piece [here](https://www.ficode.co.uk/blog/understanding-javascript-functions-in-depth).