Javascript Function

| | | | | | | |

JavaScript functions are custom blocks of code that can be reused. The functions allow the code to be more modular and are essential for object oriented programming. The functions can be defined by statements or expressions.

If you are looking to learn JavaScript and are looking to know the basics, JS functions are definitely something you have need to know. If you’ve spent time working with a programming language, you will know that functions are the building blocks of complex programs, but you may not know how they work. Functions allow you to write code once for common processes, instead of repeating it multiple times.

Functions are blocks of code that perform an action and can return a value. The functions can be customized to your needs and can be used to make your code more efficient and modular

In this tutorial, we will explore the basic functions:. How to define a function, how to call a function and when they can be useful.

How to use JavaScript define function

There are two ways to define a JavaScript function: through declarations and expressions. Let’s start with the declaration method to define a function.

JavaScript function declarations

function declarations defines a called function. To define this type of function, you must start your code with the function keyword , followed by the name of the function. Here is an example:

Function names follow the same rules as variables: they can use, underscores, leers numbers and are often written in camel cases. Then, after the variable name , you can include a series of parentheses, in which optional parameters can be kept. We will come back to this later in the article.

Then, like an or for if statement, the function code will be in braces. Here is an example of a function that will print Google to the console:

Inside the JS printGoogle () there is a console .log () statement that will be executed when the function is called. But nothing will happen until we call the function. If we want to call the function, we can use this code:

Now we are going to merge our code into a single function, then let’s call it:

The output of this function will be Google. printGoogle () will return the result when called, in this case on the last line

Now our print code is up, we can run it as many times as we want by calling the printGoogle () function.

JavaScript expression function

Another way to declare a function is to create a function expression. We can do this by assigning a function to a variable

Let & rsquo ;. S uses the same example above. Instead of declaring the function itself, we can instead assign it to a variable. Here is an example:

To call this expression, we add the line of code google () for each time we want the expression to run.

Now that we know the two ways we can declare a function, we can explore how to customize that programming function. In our code above, we created a function that prints Google on the console.

To print another name, like Facebook, we need to change the code. If we want users visiting our website to enter their preferred business name and print it to the console, our function would not work.

So we have to use parameters. If we add a name parameter to our JS function, we can print a name to the console through our function. Here is an example:

The function name is printCompany () and our parameter is called name. The parameter can then be called in the JavaScript function. In the example above, we are using the name parameter to change what will be printed on the console.log () line of code

But we haven’t defined our name yet. To define the parameter, we must assign a value when we call our JavaScript function. Let’s say your favorite business is Snapchat. We will call the function and insert the name of the company as a argument within the function call.

Here is an example:

when we run this code, the following will print:

in our example we call the function using printCompany (), then name it in parentheses. So, now we can reuse our function multiple times with different names.

It should be noted that you can use as many parameters as you like and reference them in order. We will use an example which shows in action below.

Return values ‚Äã‚Äã

In our examples so far, we haven’t returned any value. Instead of this, we printed text on the console. But, with a function, we can assign parameters to be processed and then return a value based on what’s in the return declaration.

Here is an example of a function that adds two digits and gives us the total:

In this program, our function is called and two numbers are passed by the function. When this function is executed, in the last line of our previous example, we will receive the response 3 in return. Our parameter list is comma separated

The program added 1 and 2, with the names of the parameters first and seconds and returns them to the code. If we want to see these values, we can add a console.log () around the line where we call addNumbers (1, 2).

functions Under

In ECMAScript 6, there is a more concise way to define functions known as arrow function < / a>. These are represented by the following expression:. =>

These functions are a type of function expression. Let’s take an example to show direction functions in action:

Instead of writing , we can use the arrow sign to indicate that we are proclaiming a function. There are a few subtle differences between directing functions and regular functions, but you don’t need to know them for most use cases.

, you don’t need parentheses around them. And, if you don’t work with any variables, you should include a set of empty brackets () where your variables would be declared.