DEV Community

Arina Hovhannisyan
Arina Hovhannisyan

Posted on

About functions in JavaScript, what are functions, the correct way of using them.

Functions are some sort of building blocks, i.e., sets of statements, in JavaScript, which take an input and give the desired output. It consists of a FUNCTION keyword, followed by the function’s name, a list of parameters used in it, and statements that define the function. The parameters in it are separated with commas and enclosed with parentheses. Meanwhile, the statements are held with curly brackets.
What refers to the correct way of using functions in JavaScript, there are three ways: function declaration, function expression, and arrow function.
Function Declaration - a traditional way to define the function. In this case, we should start with the keyword ‘’function’’, add its name of it, and then the parameters.
Function Expression - another way to define the function through the variables and storing the returned value in those variables. We suppose the function is a whole expression, and the returned value is in the variable.
And the last way is the ‘’Arrow function’’. Instead of using the keyword ‘’function’’ we use the arrow in order to shorten the code.
As we know, there are three types of scopes in Javascript: scope of function, block, and global. So, now, we’ll discuss the function scope.
Variables declared inside of the function become local to the function. Hence, local variables have function scope and are available and visible only inside of that function. This property allows us to use variables with the same name in different functions. For instance, variables declared with ‘’var’’, ‘’let’’, ‘’const’’ have function scope.

Top comments (0)