JS Functions or Java script Functions

A function is a code block that can repeat to run many times. To define a function, use “function function name () {}”.

function function-name () {......}

To call a function, use “function-name ();”

function-name ();

How to create function in JavaScript Ex,



<script type="text/JavaScript">

function test() { // declare a function 
alert("Visit phpgurukul.com!"); // output



<body onload="test()"> <!-- call the function -->




Visit phpgurukul.com!


"function test() {}" is a declaration of test() function. onload="test()" calls function test() after the web has loaded.

"test()" is a command to call a function named test(){}. When running the function test (), it will output “Call a function!”.

Function with Arguments

A function can have one or more arguments inside the bracket. Arguments are used to pass data to function.

function function-name (var arg) {……}

To call a function, use “function-name (argument);”

function-name (argument);




JavaScript Test


<script type="text/JavaScript">

function test(msg) { // declare a function


alert(msg); // output the value of msg




<body onload="test('Call a function with arguments')">

<!--call the function and pass arguments-->




Call a function with arguments


onload=”test(“…”)” calls function test(msg) after the
web has loaded.

When test("Call a function with arguments.") calling the function test(msg){…}, it will pass the “Calla function with arguments” to var msg. After var msg has received the data, it will pass the data inside the function. alert (msg) will use the data, and then outputs “Call a function with arguments”.

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy