Function Declaration V/S Function Expressions

Introduction to Function
A Function is an reusable code block designed to be used at multiple places and multiple without writing the complete code every time.
Functions are:
Reusable : Can be used at Multiple places
Organizable : Used for writing clear and organized code
Easily Maintainable: can be modified easily and changing code at one place will automatically change its functioning every where that function is used.
Sustainable: Consumes less energy and
Easy to Use.
Types of Function
Functions can be of different types. And they can be categorized on different Basis.
On the basis of Origin(Who made that), it can be classified into three types:
Built-in/Pre-defined Function: Provided by Browsers
User Defined Function: Defined by the User as per the Usage and requirement
Library defined Function: Functions written in Libraries we use.
On the basis of Arguments(values required by the function to work), it can be divided into:
Non-Argument Functions: Functions having no arguments or requires no parameters(values we provide to the function) while calling that function.
Argument Functions: Functions having arguments or requires parameters while calling that function.
We can categorize function on lots of different basis and these two are just examples. So let’s move on to the next Topic
Uses of Functions
We can use functions to apply a code block that can consist any loop or method or even a function, and we can write any function one time, and can use them multiple times. Example of an Function:
This function is taking an array is parameter(as we written an argument in paratheses) and then the “for.. of” loop will be started which will give all the elements of that array one by one and then we just printed those elements.
Let’s check how to use this function:
Here we written the same function, and then we declared an array with name “array1” and finally we pushed that array into our function as the parameter. And our function worked as we want and this had given this output:
Similarly we can create any Function and we can use that function multiple times.
Now let’s talk about the main Topic of this Blog:
Function Declaration V/S Function Expressions
Function Declaration/Function Statement
It’s just a way to define any function but with any name, but generally we name it on what that function gonna to perform or work. And the Basic syntax to define it is:
where “greet” is the name of this function and there is no arguments inside the parentheses “()” and function is returning “Hello!”
But before we know the difference between them, firstly we had to know what they are, and then we will understand their difference.
Function Expression
Its just Writing a function without name and storing that function in a variable. They are usually used in defining functions inside objects. Basic syntax of an Function expression is:
In this case, function is stored inside this greet variable and this will return “Hello!”.
Difference
Property | Function Declaration | Function Expression |
has Name | Yes | No, stored in variable that have name but not of the Function |
hoisted | Yes(can be used before even they initialized) | No |
Syntax | function name (argument){function body} | let name = function (argument){function body} |
Use Case | Increases code reusability, and makes itself available globally(every where) | limited scope(unavailable globally), and can be used to write nested function and on-time inline function. |
There are lots of more minor differences, but these are the major differences that you had to know.
Hope you understood the major difference between them. I hope this blog was helpful for you. Make sure to hit on the like icon, if you found reading this blog helpful.
Subscribe to my newsletter
Read articles from Manish Saw directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
