Home | | Web Technology | Javascript Functions

Chapter: Web Technology : JavaScript (JS)

Javascript Functions

A function will be executed by an event or by a call to the function.

JAVASCRIPT FUNCTIONS

 

A function will be executed by an event or by a call to the function.

 

JavaScript Functions

 

To keep the browser from executing a script when the page loads, you can put your script into a function.A function contains code that will be executed by an event or by a call to the function.You may call a function from anywhere within a page (or even from other pages if the function is embedded in an external .js file).Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that a function is read/loaded by the browser before it is called, it could be wise to put functions in the <head> section.

 

How to Define a Function

 

Syntax

 

function functionname(var1,var2,...,varX)

{

 

some code

 

}

 

The parameters var1, var2, etc. are variables or values passed into the function. The { and the } defines the start and end of the function.

 

Note: A function with no parameters must include the parentheses () after the function name.

 

Note: Do not forget about the importance of capitals in JavaScript! The word function must be written in lowercase letters, otherwise a JavaScript error occurs! Also note that you must call a function with the exact same capitals as in the function name.

 

JavaScript Function Example

 

<html>

<head>

 

<script

type="text/javascript"> function displaymessage()

{

alert("Hello World!");

}

 

</script>

 

</head>

 

<body>

The return Statement

 

The return statement is used to specify the value that is returned from the function.So, functions that are going to return a value must use the return statement.The example below returns the product of two numbers (a and b):

 

Ex:

 

<html>

<head>

 

<script

type="text/javascript"> function product(a,b)

 

{ return a*b;

}

</script>

</head>

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web Technology : JavaScript (JS) : Javascript Functions |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.