First Class Functions

Vchanduu
4 min readJan 2, 2021

In this blog I want to discuss about functions and different types of functions in JavaScript.

Function Statement

The function statement declares a function. A declared function is “saved for later use”, and will be executed later, when it is invoked (called).

Function Declaration is also known as function Statement

This way of creating a function is called function Statement

Function expression:

functions are like heart like JavaScript, beautiful feature of a function is that you can assign it to a variable

This way of assigning function to variable is called function expression , here function acts as a value.

Now what is the difference between function Statement and function Expression ? (Think for a while )

Yes, you are right, it’s hoisting.

If I try to invoke both the functions , I will print the values in console

Suppose what if I call the functions even before creating it , let’s see

So, can you guess what’s the output here (think for a while)

So, one will throw out an error , which one ? yes you’re right the b, it throws a “Uncaught Type error : b is not defined” so that is the difference between a function statement and a function expression, so during the hoisting phase or memory creation phase , function javascript is created a memory and that function is assigned to javascript , but in case of function expression b is treated like any other variable, it is assigned undefined initially until the code hits its line.

Anonymous function :

A function without a name is called anonymous function . Anonymous functions are used as values , i.e. you can use it to assign it to some variables. In the above snippet the function which we assign to variable b is an anonymous function . One more point is if we don't assign it to some variable it throws an error .

Named Function Expression:

It’s same as function expression , but the difference is we give names to functions too.

There was an edge case here, what happens if we invoke xyz , can you make a guess, yes it throws an error.

so why it gives xyz not defined, because xyz is not created in the outer scope(global scope) but it is created as local variable , if we try to access xyz inside that function it will give the function as output, so its creating local scope to that function.

Difference Between Parameters and Arguments :

Most of the programmers use parameters and arguments as interchangeable terms, but these two are different .

In the above code snippet , param1 and param2 are knows as parameters and and b(1,2) here 1,2 are knows as arguments .

First Class Functions:

The ability of functions to be used as values and can be pass the function as an argument to another functions and can be returned from the functions is known as first class functions.

Don’t go away , I will give some examples

That’s why first class functions are also knows as “First class Citizens”

--

--