Javascript Functions – Learn How To Create Functions In Javascript

What are Javascript Functions?

Javascript functions are the code blocks which declared once and executed many times.

Why we use functions?

Without the functions, you can also write the javascript code. But you find situations that you have to write some code again and again. In some cases, you have to keep the code blocks separately.

All these cases may make your code too large and may give you a bad coding experience.

To handle these type of situations, javascript functions are invented.

Functions are the code blocks which can be created once and executed as many times as you want.

To execute a function you have to invoke it.

How to create a javascript function?

To create a function we use function keyword along with function name and a couple of flower brackets { }.

Syntax :

function function_name(){
     //function body
}

Example :

function my_name(){
       console.log("Joovin South");
}

Just now, we created a function my_name. If we call my_name, it will print Joovin South.

To invoke a function, you have to write the function name with a postfix of a couple of parentheses().

Syntax :

function_name();

For example :

function my_name(){
       console.log("Joovin South");
}

//calling function my_name

my_name();

Creating function in a variable

You can also create a function in a variable. i.e you can store an entire function in a variable.

Syntax :

var varaible_name = function(){
    // body of the function
}

Example :

var my_name = function(){
    console.log("Joovin South");
}

my_name();
//Expected output is Joovin South

Passing parameters to a function

Parameters are nothing but values like strings, numbers, booleans which we pass to a function to use them in program logic.

First of all, we have to add parameters to the function.

Adding parameters means, write the parameter names in the middle of the parentheses.

Each parameter should be separated with a comma (,)

Syntax :

function fuction_name(param1,param2,param3){
       // body of the function
}

Example :

function Person(name,age,job){
        console.log(name);
        console.log(age);
        console.log(job);
}

In the above example, we added a 3 parameters name, age, job to the function Person.

Now the parameters don’t have any value.

So, to give values to parameters, we have to pass them while calling a function.

We call these values as function arguments.

Example :

function Person(name,age,job){
        console.log(name);
        console.log(age);
        console.log(job);
}


//calling function with parameter values

Person("shiva",23,"web developer");

Code Explanation :

In the above example, we pass 3 parameter values "shiva", 23, "web developer" to the function

So, when we pass parameter values through the function calls. It will automatically assign values to parameter names in sequence.

Means the first parameter_value of the function call will assign the first parameter_name of the function and the second parameter_value of the function call with assign the second parameter_name of the function.

And the same process happened to the remaining parameters.

In the above example, value “shiva” stored in name, and value 23 stored in age, and value “web developer” stored in the job.

If you pass extra values than the required, the javascript interpreter doesn’t consider that value.

Example :

function Person(name,age){
     console.log(name);
     console.log(age);
}

Person("Joovin South",23,true);

Expected output :

Joovin South
23

If you didn't pass a value to the function parameter, makes the parameter value to undefined.
function Person(name,age,job){
       console.log(name);
       console.log(age);
       console.log(job);
}
Person("Joovin South",23);

Expected output :

 Joovin South 
 23 
 undefined

If you have any doubts feel free to comment below. If you like this article please share it with other learners.

Click here to read more about javascript functions.

Also, try my other javascript tutorials.

  1. Learn Javascript in one day
  2. Implementing lists in javascript

Thanks for reading!

Keep [email protected]

Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
Yes0
No0
Powered by Pixelbart

Leave a Reply

Your email address will not be published. Required fields are marked *