Understanding Functions in Javascript

          What is Function

Functions are sub-programs that can be reused either within the program itself or in other programs. A function in Javascript is an object. Because it has properties and methods.

          How to create a Function in Javascript

  1. Creating Functions in the usual way

This method is most often used, especially for those who are just learning Javascript.

  • Creating Functions with Expressions

We use a variable, then fill it with a function. This function is actually an anonymous function (anonymous function) or a function without a name.

  • Creating Functions with Arrows

This method is often used in today’s Javascript code, because it is simpler. However, it is difficult for beginners to understand. This function is starting to appear on the ES6 standard. Actually almost the same as using expressions. The difference is, we use arrows (=>) instead of functions. Creating a function in this way is called an arrow function

  • Creating Functions with Constructors

This method is not recommended because it can affect the performance of the javascript engine in the browser.

            How to call and Execute Function

Now that you know how to create a function, how do you call it? We can call a function in Javascript code by writing the name of the function.

            Function with Parameter

Parameters are variables that hold values ​for processing inside the function.

          Function that returns value

In order for the result of processing the value in the function to be used for the next process, the function must return a value. Returns the value of the function using the return keyword, followed by the value or variable to be returned.

        Conclusion and Closing

Functions are needed even make javascript very powerful, for example from the beginning we use alert(), prompt(), which are functions provided by javascript itself. This is just the basics.

The need for digital IT is needed in daily activities, Bead IT Consultant is the right choice as your partner, visit our website by clicking this link: www.beadgroup.com