In this article we will discuss about why use fuctions in javascript and what are the javascript function types. If you are learning basics of any programming language defenitly it includes function. So this article i will briefly explain about javascript fuction and all the key point you need to know about javascript function. first we will look about what is the function in programming. when you writing some code you want to repeat some process again and again you can do it using fuctions. simply you can create a fuction and inside it you can declare logic of the process. And after you can call it on any where in the codebase. Let’s look simple example for the fuction.
See this image this is a example for simple javascript function. If you want to import this function simply write the function name and inside the () you want to add the parameter that include when calling this function. Like that we can create lot of function as wants. Next we will look about some types of javascript functions. Mainly have 3 types of function types in javascript. These are function declaration, function expression and arrow function. Let’s we will discuss about one by one.
Function Declaration
This is the simple method we can declare in function. Just type only the function keyword and the function name and after put the () symbol and inside the () symbol we can include the parameters. Then after we can open {} and inside the {} simply write the logic of the function. See this below image for more details.
See this image, first i declare function name it is myFirstCode inside this function i have write the logic of function. after i close the function bracket and again i want to call the function simply i typed the function name. Using this function i can use again and again this function by calling to it.Next we will look about second type of function it is function expression.
Function Expression
Function expression in JavaScript is a way to define a function as part of an expression, usually by assigning it to a variable. Simple meaning this type of function we don’t input the function name we assign to some variable. When ever we want to call the function in that moment we will call that variable this function assigned. See this example image.
This example function we assign to variable that name is greet. And after calling the function inside the brackets i will pass some parameter it name is ‘Alice’. After we can get the output ‘Hello, Alice!’ like that. Next we will look about last type of javaScript function type it is Arrow function.
Arrow Function
Looks like arrow function and function expression declaration is same. The difference is in arrow function we use ‘=>’ this symbol for declaring the function. The arrow function looks like this.
Arrow function also like function expression type first declare some variable and assign the function to this variable. But we can declare the arrow function without the variable also. And another thing is simple arrow function no need to write the {} curly brackets. The function can be declare without the curly brackets. See this below example image for understand this more.
This example image first function declare the normal regular function and second function you can see the simplified arrow function. In the simplified arrow function you can see it has no write {} after the arrow symbol.
So in this article we will discuss about what are the functions in javascript and types of javascript function. I will give the very basic idea of the function. i think now you can get a better understanding about javascript function and types. If you have any question regarding this please put in the below comment section. So good Bye!