JavaScript Functions

JavaScript functions are used to execute part of the code repeatedly. They are best used in different scripts to manage frequently used script parts. We will start with simple function syntax.
function name(){
//Code inside function
}
Let us start with an example.
<script language='JavaScript' type='text/JavaScript'>
function my_fun(){
document.write('Welcome to plus2net');
}
my_fun(); // Call to function
</script>
By adding function we can't use it. We need to call the function to execute the code kept inside the function. The line at the end of the above code call the function to execute it.

Passing a string variable to function.

We can supply a variable to the function and use the same to process inside it
<script language='JavaScript' type='text/JavaScript'>
function my_fun(str){
document.write('Welcome to ' + str);
}
my_fun('plus2net');
</script>
Getting return variable from function.

We can input two integers to function and get the sum of the two integers as return variable to calling script. We used parseInt function to convert input
<script language='JavaScript' type='text/JavaScript'>
function my_fun(v1,v2){
var v3= v1 + v2;
return v3;
}
var result=my_fun(5,8);
document.write('Sum of two number is ' + result);
</script>

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer