Java Script functions
Tags: javascript, functions
Published: 2022-11-09 19:59:03


Hello everyone and I’d like to welcome you all to my blog. During my previous posts we discovered the basic knowledge required for learning Java Script. We’ve discussed variables, scopes and what we can do with variables. In this post, we will learn about functions. The functions in Java Script are very identical to the functions we all know from mathematics. Basically functions are statements that perform some task or compute some value. Or we can say that it is a code implemented to perform a particular task. A function performs the task when we “call” it. It needs to take in some input and produce an output. For a function to work, we need to define it in the scope from which we want to call it. In simple and clear language a function consists of several statements but it displays only a single statement. We write a function in the following way. First we write the keyword function, then we write the name of the function, which can include letters, digits, underscores, and next to it we write the parameters of the function inside braces.  Finally we write the main body of the function inside the curly parenthesis. After all of this, we call the function and it gives us some value in return. Let’s look at an example and understand the concept of functions better.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4slr5uge7hosu2i6mg3.jpeg)

So, let’s understand this example. The function reads the steps starting from the first line. So, first it understands that we have assigned a function to the variable x. It contains two arguments.  It sees that there is a condition of multiplying the arguments and returning them to us.  Finally, it reaches to the point where it prints the value that it needed to return to us. The return statement indicates that the function has ended and any other code coming after that will not be executed.

Functional programming can seem a bit complex but in reality, functions are our friends as they have lots of benefits. For example, it is reusable. We can call our function numerous times and save many lines of coding. So we don’t have to do code repetition. Furthermore, it can break down a large program into smaller programs and make it easier to read.

There are several ways of declaring functions. A function declaration is simply the process of giving instructions to the function for it to implement them. A function can also be declared by function expression. This kind of expression uses variables to declare the name of the function. It comes before the word “function”. The function is declared by calling it. This specifies the name of the function and it helps us in tracing bugs. There is another type of functions called Construction functions. It is when we create several objects which are similar to each other. We denote it with the keyword “new Function”. Construction functions are getting executed in the global scope.

Arrow functions are somewhat similar to function expressions. This is the case when we don’t use the keyword “function”. We name our function, list the arguments inside the brackets and then we use an arrow sign and indicate the values in curly parenthesis. Arrow functions are more convenient as they save time.

So, these were the basic things you all need to know about Java Script. Hope this helped you. Thank you for your attention.