Skip to content

JavaScript Function

Why the function?

The function is a group of codes designed to perform a specific task. This function is written once and can be called anywhere in the program after it is written. We just need to enter the name and parameters of the function. This way we don’t need to write unnecessary codes when writing programs. Only when we assign functions to where it is needed solves this task. At the same time, functions are objects.

function name(parameters) {
  // the required codes are written here

Rules for creating JavaScript function

1- The name of each function must be different. Two functions with the same name cause a fault.

        function writeName(name) {

        function writeName(name) {

2-Comma separated parameters list.

        function user(name,surname,age){
                "name: " + name +
                "surname: " + surname + 
                "age: " + age

3– A list of statement composing the body of the function enclosed within curly braces {}.

What is the parameter?

Additional information is given to the parameters in the function. For example, in the example above, “name” is a parameter. When we call the function, it is not enough to just write the name. If you have the parameters of the function, you need to write the parameters. The parameters can be any value (string, number etc …).
The queues are important if there are multiple parameters. In the above example, “name” and “surname” are a paramtred. And in these parameters, the “name” parameter is ahead of the “surname” parameter. Therefore, when the function is called and the parameters are started to be written, “name” is written first. Then “surname” is written.

       function car(model, date) {
                "model: " + model + "<br>" + 
                "date: " + date

For example, when calling the function, I have to enter the parameters respectively.

car("tesla x",2018)


model: tesla x
date: 2018


We want to rotate the values ​​in some functions. In such cases (usually) the value is returned at the end of the function using the return statement. An optional post is an expression. So it’s not a rule of javascript.

        function numbers(x, y) {
            return x + y;

        let arr = numbers(2, 5);



Self-Invoking Functions

Sometimes functions have to automatic call themselves. In such cases “self-invoking” is used. Let’s understand the logic with a small example.

        (function () {
            document.write("Self-Invoking Functions")

JavaScript Function Properties And Methods

Function arguments: An array corresponding to the arguments passed to a function.
Function caller: Specifies the function that invoked the currently executing function.
Function length: Specifies the length of the function.
Function name: The name of the function.

Function apply(): Calls a function and sets its this to the provided value.
Function bind(): Creates a new function which, when called, has its this set to the provided value.
Function call(): Calls a function and sets its this to the provided value.

Javascript functions

As a result:

  • A function is a subprogram designed to perform a particular task.
  • Functions are executed when they are called. This is known as invoking a function.
  • Values can be passed into functions and used within the function. The name of the value is called a parameter. The actual value itself is called an argument.
  • Functions always return a value. In JavaScript, if no return value is specified, the function will return undefined by default.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: