Functions in JavaScripts
A function is a set of code that can optionally accept parameters and return a value. A JavaScript function may take zero or more parameters.
A function has three major parts:
- The name of the function
- Parentheses include Arguments, but Arguments are optional, An An argument is simply a value (or a reference to a value) that the function is expecting. A function might expect zero, one, two, three, or more arguments.
- Body of the function between curly brackets (opening and closing)
Every function is stated with function keyword. The syntax of JavaScript function is as follows:
function
function_name (optional arg1, arg2) {
block of code
}
block of code
}
The parameters of function are optional. you can use function without paramters. For example:
function topwebsite() {
document.write("http://www.programmersbank.com");
}
topwebsite();
document.write("http://www.programmersbank.com");
}
topwebsite();
Output:
http://www.programmersbank.com
Another example of JavaScript with argument is:
function
topwebsite(website) {
document.write(website);
}
document.write(website);
}
In this examples you have used one argument $website.
topwebsite("http://www.programmersbank.com");
Note: A function is a block of code that is not immediately executed but can be called by your scripts when needed.
function
valueExpressions(value1,value2,symbol) {
symbol;
document.write(value1."+".value2);
}
symbol;
document.write(value1."+".value2);
}
