Learning Javascript Functions and Examples of Its Application

In this article, we will find out with regards to capacities in the javascript programming language, where works in javascript are one of the basic structure blocks in the javascript programming language. A capacity in javascript is like a strategy—a bunch of articulations that play out a task or compute a worth, yet for a method to qualify as a capacity, it should have the option to take some information and return yield where there is some unmistakable connection between the info and the yield. 

To have the option to utilize a capacity, you need to characterize some place in the extension where you need to call it. Moreover, in the event that we will forge ahead the conversation of Function Parameters in Javascript

See : Types Of Pragramming Language

Defining Functions in Javascript

A definition of a function (also known as a function declaration, or a function statement consisting of the function keyword, followed by:

  • Function name.
  • The list of parameters of the function, enclosed in parentheses and further separated by commas.
  • A javascript statement that defines a function, enclosed in curly braces like this {….}.

For example, the following code defines a simple function named square, for an example of its implementation as follows:

function square(number){
      return number * number;

The square function takes one parameter, which is called number. A function consists of a single statement that says to return the function parameter (i.e., number) multiplied by itself. The statement i.e. return serves as returning the value returned by the function:

return number * number;

Primitive parameters (such as numbers) are passed into the function by value, the next value is passed to the function, but if the function changes the value of a parameter, this change is not reflected globally or in the function call.

If you pass an object (that is, a non-primitive value, such as an array or a user-defined object) as a parameter and also a function changes the object's properties, the change is seen outside the function, as shown in the following example:

function namaFunc(theObject) {
  theObject.make = 'Yamaha';
var mycar = {make: 'Suzuki', model: 'Accord', year: 1998};
var x, y;
x = mycar.make; // x mendapatkan nilai "Suzuki"
y = mycar.make; // y Mendapatkan nilai "Yamaha"

Next we will learn about ways to create functions, please refer back to the following discussion.

4 Ways to Write Functions in Javascript

There are 4 ways that we can do to create functions in javascript programming:
  • By using the usual method.
  • By using expressions.
  • Use arrows like (=>);
  • and we can also use Constructor.
Next we will try to implement from the four ways that the admin has mentioned above.

1. Creating Javascript Functions the Ordinary Way

By using this usual method, of course the most frequently used method, especially for those who are just learning the javascript programming language. For an example script, please look at the code below.
function namaFungsi(){
    console.log("Welcome to Serverkoding");

Next we will try to create a function using an expression.

2. Create Javascript Functions using Expressions

For how to use expressions in javascript functions, it's certainly different from before, where we have to create a variable first and follow it by function(). For an example of its implementation as follows:

var namaFungsi = function(){
    console.log("Welcome to Serverkoding");

In the script above, we use a variable, then the variable is filled with a function. The function is actually an anonymous function or we can know it as an anonymous function.

3. Create Javascript Functions with Arrows

Creating functions in javascript using arrows is of course often used in today's javascript code. Because the writing is simpler. However, it will be very difficult to understand for a beginner who is just learning javascript. This function starts appearing when the ES6 standard appears.

For an example of implementing a javascript function with arrows like the following:

var namaFungsi = () => {
    console.log("Welcome to Serverkoding");
// ini merupakan contoh lainnya dari penggunaan fungsi dengan tanda panah
var namaFungsi = () => console.log("Welcome to Serverkoding");

Actually, the syntax for writing programs is almost the same as using expressions. It's just that, we use the arrow (=>) instead to replace the function.

Creating a function in this way is called an arrow function.

4. Creating Javascript Functions with Constructors

Making javascript functions using constructors is certainly not recommended by Mozilla Developers. Because it looks more complicated and less good. Because the function is made in the form of a string which can interfere with the performance of the javascript engine.

An example of an implementation using a constructo javascript function is as follows:

var namaFungsi = new Function('console.log("Hello World!");');

Here are 4 ways to create functions in javascript, for those of you who are new to javascript, the admin recommends using the first method first. Furthermore, if friends are used to it then try using the 2nd and 3rd method.

Next we will learn how to call or execute functions. Please refer to the following discussion.

How to Call or Execute a Javascript Function

After we learn about how to create functions in the javascript programming language, then of course we must also know how to call them.

We can call a function in a javascript script by writing the function name like this:


Examples of implementation in programs such as the following:

// membuat fungsi
function sayHello(){
    console.log("Hello Kawan Serverkoding");
// memanggil fungsi
sayHello() // maka akan menghasilkan -> Hello Kawan forumkoding

In addition to using the above method, we can also call functions via the event attribute in the HTML framework.

Function Parameters and Arguments in Javascript

In the previous discussion, you have learned that in javascript functions can have parameters, an example of writing a function parameter is as follows:

function functionName( parameter1, parameter2, parameter3) {
  // code to be executed

Function parameters are names listed in the function definition. Function arguments represent values ​​passed to (and accepted by a function).

Rules In Parameter

A function definition from javascript does not specify a data type for a parameter. Functions in javascript do not perform type checks on passed arguments.

The javascript function also doesn't check the number of arguments received.

Javascript Default Function Parameters

If a function is called with missing arguments (that is, less than declared), the missing value is set to undefined.

Sometimes it's acceptable, but sometimes it's also better to apply default values ​​to parameters, here are some examples of implementing JavaScript's Parameter default function.

function myFunction(x, y) {
  if (y === undefined) {
    y = 2;

In ECMAScript 2015 it is possible to allow parameter values ​​by default in function declarations:

function (x, y = 2) {
  // function code

See : Filter Array in Javascript


Functions defined by the user are very important to learn because using functions can certainly shorten script writing when we create a program, thank you for visiting and reading this article, hopefully this article can increase your knowledge about Learning Functions in Javascript.

Hopefully Helpful, greetings Success

By: Muhammad Rizal Supriadi

0 Response to "Learning Javascript Functions and Examples of Its Application"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel