What is JavaScript Function

What is JavaScript Function

What is JavaScript Function

In this Blog Post, we can learn about “JavaScript Function Definition”.

What is a Function?

A function is a block of code which is designed for performing some task. We can call the function anywhere in the program.

The function will allow programmers to divide a large program into a number of small functions.


JavaScript Function Defination

A Javascript function is a block of code which can be used to perform some task.

The function can be defined in javascript by using the function keyword. And it is followed by a function name and it is with a list of parameters.

Syntax:

<script type = “text/javascript”>

<!–

function function_name(Aurgument-list) {

statements to execute

}

//–>

</script>

Ex:

<script type = “text/javascript”>

<!–

function sample() {

alert(“Hello there this is a function definition”);

}

//–>

</script>


Calling a Function in JavaScript

The function can be called at the time of the event occurs.

To call a javascript function, we simply need to write the function names.

<html>

<head>

<script type = “text/javascript”>

function sample() {

document.write (“hey friends”);

}

</script>

</head>

<body>

<p>Click on the following button for calling the function</p>            <form>

<input type = “button” onclick = “sample()” value = “hello there!”>

</form>

<p>Use different text in write method and then try…</p>

</body>

</html>


Function parameter in javascript

Before this, we can see a function with no parameter but in this section, we can see a function with parameters.

The parameters can pass at the time of the invoking function.

The function can take multiple parameters which are separated by a comma.

Ex:

<html>

<head>

<script type = “text/javascript”>

function sample(name1, age1) {

document.write (name1 + ” is ” + age1 + ” years old.”);

}

</script>

</head>

 

<body>

<p>Click on the following button for calling the function</p>

<form>

<input type = “button” onclick = “sample(‘harsh’, 19)” value = “Hello”>

</form>

<p>Use different parameters inside the function and then try…</p>

</body>

</html>

Output:

harsh is 19 years old.


Function Return

When the javascript reaches a return statement, then the function will stop the working.

This is the last statement of function.

Ex:

<html>

<head>

<script type = “text/javascript”>

function concatenatestring(first1, last1) {

var fullname;

full = first1 + last1;

return fullname;

}

function secondFunction1() {

var lastresult;

lastresult = concatenatestring(‘harsh’, ‘sonawane’);

document.write (lastresult );

}

</script>

</head>

 

<body>

<p>Click on the following button for calling the function</p>

<form>

<input type = “button” onclick = “secondFunction1()” value = “Call the Function”>

</form>

<p>Use different parameters inside the function and then try…</p>

</body>

</html>

 

Also Read This:

What internet of things?

Activity Lifecycle in Android

Harsh

Hey Friends, Welcome to thetechbuddy. This blog is about technical or programming knowledge.

Leave a Reply