DEV Community

Cover image for Functions in JavaScript
Sona Barseghyan
Sona Barseghyan

Posted on

Functions in JavaScript

Hello again! I hope you're doing great digging further into the amazing world of programming. As you remember, in our last post we were talking about variables in JavaScript. Just to recap what we understood: we can declare variables with var, let, const. The values of var and let can be reassigned, while values of const cannot.
At the end of my previous blog post I promised to talk about the functions. So, this is the moment you have waited for!!!
Functions in JavaScript are used to perform a particular task in the program. Like variables, functions have names that can contain digits, underscores or dollar signs. We denote a function by adding () at the end of it and then writing our piece of code in curly parentheses {}. In the parentheses () we write the parameters with which we are going to make operations. Parameters are the names of the values we are going to use. The exact and real values we are using in order to get a result from the function are arguments. The function will execute when we somehow create a statement to call it. After we "call" the function, it "returns" us a value.
Have a look at a function example:

Image description

Now let's understand what we did here step by step. First of all, we declared a variable x storing in it the value of a function with arguments 10 and 5, which will later be returned to us. Then we restate the function with its parameter, mentioning the condition, in our case adding the 2 values. And at the end we use the function console.log() to print the output that we got from the operation made in the function.

The variables that we declare in the function become local to them. And this means that those variables are accessible only in the scope of the function. In other words each function creates a new scope. Therefore, we are allowed to use the same name of variable in different part of a program: in different functions.

Image description

Let's observe the example above: one can see that first we want to print the value of variables declared in the function, and it does so, and only then when we use console.log() outside the function it gives us the values of the variables declared outside the function.

We can create functions in 3 ways: function declaration, function expression, arrow function.
1)We start declaring a function using the keyword “function”. After that we write the function name and the parameters.
(I am going to illustrate the same example given at the beginning of my post so that you can see the difference between the ways of creating functions).

Image description

2)Function Expression is a way to define a function by defining a function using a variable and storing the returned value in that variable.

Image description

3) While creating an arrow function we save time.

Image description

So, this is all I have prepared for you about functions. Have a good day and rock with your coding skills!

Top comments (0)