function- building blocks of the javascript

DIFFERNCE between the FUNCTION DECLARATION and the FUNCTION EXPRESSION
Disclaim( before going to understand this difference I think I should give you a glance on the topic what is a function. As being a coder you all should a basic logic behind, printing the hello world, but if I say you print this hello world 1000 times (my blog my choice), despite knowing the logic you would not do and some of the students who will prefer chat gpt to do this task, will end up seeing that gpt is also using function to complete this task (actually it will use loop but as of now we are talking of function so I said function, plz don’t be angry on me for this ). so a function is a block (ek boundary jismai ek particular logic likha ho for completion of any task) of code that can be usefull when we want to reuse that block of code multiple times in our code (like in my example), it also helps my code to look readable (obviously if a write a code to print 1000 hello world , its debbuging would also be as slow as it’s execution) , saves our time of writing that same code multiple times, it make debugging easy as we know which function is wrong if any error occurred we could be able to know in which function we have occurred .
working of the function : function enclose the code in a block surrounded by curly brackets and when we call that function with its name then the compiler goes to that function and execute the logic by this we don’t have to write the logic again and again we can simply call the function)
SYNTAX : function function_name (parameter ){ function body } -// function declaration
function_name( arguments) -// function callingnow talking on the difference :
A.) function declaration is the basic procedure of using a function - means imagine when you are using your computer so how would you tell your computer that you want to use your chai aur code app ( by clicking the app) aise hi in code when you want to use function for its feature you want to tell the compiler that we are using the function by declaring the function and the syntax of that is given above or u can do gpt.eg: let text = “hello chai aur code, how are you”
function printhello ( text -//here while declaring a variable we don’t have to use the let keyword as function do it by default ) {
// this is the body of the function enclosed with the curly brackets
console.log( text)
}now if we want to execute he function we just call it :
printhello( text ) -// here we paas the argument which means predefined values which we are going to use in the function and it is declared only when the parameter is created
B.) function expression is the code logic that we are writting inside the fucntion body or it is the block of code that we want to execute multiple times ( jisse baar baar likhne mai alas ata hai ) . it is also called the function body and is enclosed within the ( {}) curly brackets. one of the important point in the function expression is that it has scope - it means ( when we declare variable inside the inside the scope of the function means inside the curly brackets it cannot be used outside that scope, means in the global scope)
eg:1 let text = “hello chai aur code, how are you”
function printhello ( text ) {
// this is the body of the function enclosed with the curly brackets
console.log( text)
let name = “rounit singh”
}
printhello(text) o/p - hello chai aur code, how are you
console.log(name) o/p - error** (variable is not defined )
eg: 2 let text = “hello chai aur code, hope you are fine 2”
function printhello ( text -//here while declaring a variable we don’t have to use the let keyword as function do it by itself ) {
// this is the body of the function enclosed with the curly brackets also called as function expression as it contains the business logic
console.log( text)
}
Arrow function: these are just a fancy name (as said hitesh sir ) for fancy feature of function, in this the difference is in the syntax and the return property of the function. As suggested by the name the arrow function include the arrow in it’s syntax. In this the use of the function keyword (boring part ) is not necessary.
it has multiple types of function in this :
A.)) implicit return - in this the output of the business logic is returned by default so we generally store it in a variable and it’s syntax does include the curly brackets for the body of the function.
eg: let FunctionOutput = (parameter) => “hello chai aur code, how are you” ;B:)) arrow function - it is also a similar kind of function but the main difference is the presence of the scope (curly brackets) in the syntax and the return method.
eg : let functionOutput = (parameter )=>{
return “hello chai aur code, how are you” }
in this eg we have to use a return keyword as to return the output of the business logic, which is similar to the use case of the normal function but as i said the syntax which is present in the arrow function makes it different.
thank you - always open for the feedback.
Subscribe to my newsletter
Read articles from Rounit Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
