Function In JavaScript

Ravindra Rajpoot
2 min readDec 10, 2020

First, we discuss the function. what is the function?.
The function is a set of statements that perform a task or calculate a value.
Example:
Suppose We want toasted bread. How can do that? In this process, we will require many things like bread, toaster machine so the toaster machine will behave like a function because it accepts bread and gives the toasted bread. the bread behaves like an argument that is put inside the function.

Function Declaration:-
A function definition also called function declaration.
first, we write the function keyword then write the name of the function then we write parameters inside the parenthesis. when the parameters more than one then we used a comma for separating both arguments. after that, we will write curly braces and inside curly braces, we will write code. that is also called a function body and the whole process is called a function declaration.
Example: We can see the above image there first I write a function keyword after that I write function name that is add, after that, I write parameters inside parentheses that is a and b after that I write function body between the curly braces.
inside the function body, I used the return statement. that is return a value if the function does not have a return statement it will return undefined by default
one Important thing we can only use the return statement inside the function body and after the return, statement our code is not executed.

Function Expression:-
A function expression is a way of storing a function as a value inside a variable that is called a function expression.

we know that the variable stores the only value on the right side of the assignment operator. and function is an object and the object is an expression that always returns a value.
In the above image, We can see there, I declare a variable with name add, and after the variable name goes the assignment operator. and we know the right side of assignment operator we can define only value so function always returns values then we can store function right side of an assignment operator.

Why do we use the function?

We can avoid repetition of code by using the function.
We can make our code reusable by whing the function.

--

--

Ravindra Rajpoot
0 Followers

I have completed Btech. Now i am taking software traning to altcampus