Function in Javascript. Are you aware of it? – Learn Javascript Online

Hey guys, whats up? After a long time, I am with a new post that is to Javascript function. Basically, in this session, you will get to know that how to use javascript functions? So let’s get started with some basic idea of function.

Okay, that first question that comes to our mind is that what is function and why we use it? Let’s check it out and find the real theory of it.

Function

A function is a group of codes which can be reused in the program at different places. We use this tactic to avoid misunderstanding of the codes. By using the function you can use code very efficiently and can manage it very easily. Like we have a function like alert etc. which are pre-written in the core libraries of the programming language.

To avoid the code density we just use a small function name alert to call those all code statements. Which is very easy then writing code again and again.

What is the syntax of function? Are you having the same question? Alright! Let me explain it to you now.

To make any function during programming in Javascript you have to just write a simple keyword function and then the function name like myName and two braces which are used as () (function myName()) inside the braces of function you can pass the parameters.

SYNTAX IS HERE

<script type="text/javascript">
      function functionname(parameter-list)
      {
         your statements to execute...
      }
</script>

Use of function will be cleared when I will give you a simple example of myName() function. So let’s start with it. Have a look at the following image:

Function example in Javascript by Rahul Rahi

Function example in Javascript

Guys I have shown an example of the function above from where you can get the idea of usage of Javascript function. But if you still have any doubt, just post your comment at below comment section I will try my best to resolve the issue.

CODE OF ABOVE EXAMPLE

<!DOCTYPE html>
<html>
<head>
 <title>Use of Function in Javascript</title>
<script type="text/javascript">


 function myName(){

alert("Hey I'm Alert! How are you?");
 }

</script>
</head>
<body onload="myName()">

</body>
</html>

Now we have one more thing to discuss in the function session i.e. return statement. Many people get confused in this return word. Is it really confusing? Let’s check it out…

A return statement is used to just simply return a value from a function.

A simple program of return:

<!DOCTYPE html>
<html>
<head>
 <title>Return Function</title>
<script> 
function getInfo(){ 
return "I'm return! And I'm returning a text value."; 
} 
</script> 
<script> 
document.write(getInfo()); 
</script>

</head>
<body></body>
</html>
Return Function in Javascript example by Rahul Rahi

Return Function in Javascript

Function Parameters

Function parameters can be used in the javascript functions as function fun(name, type). How to pass the parameters in the function will be cleared in the following example.

Function Parameters Example by Rahul Rahi

Function Parameters Example by Rahul Rahi

CODE IS HERE

<html>
 <head>
 
 <script type="text/javascript">
 function myBio(name, age)
 {
 document.write ("My name is " + name + " and my age is " + age);
 }
 </script>
 
 </head>
 <body>
 
 
 <form>
 <input type="button" onclick="myBio('Rahul', 22)" value="My Biodata!">
 </form>
 
 </body>
</html>

 

Okay, so this is for the today’s session let me take a break for the next one. Guys don’t forget to like Codingparks.com on facebook and follow on twitter to stay updated.

 

Leave a Reply

CONTACT ME