function functionName(){
// javascript code you want to run
// ...
// ...
}
- The keyword function lets the interpreter know you are creating a function.
function printToday(){
var today = new Date(); // create a Date object and put it into the variable today
document.write(today.toDateString() + " "); // convert to string the date represented by variable today
document.write(today.toTimeString()); // convert to string the time represented by variable today
document.write("<br>");
}
<!DOCTYPE html>
<html>
<head>
<title> The Title of this HTML Page </title>
<script>
function printToday(){
var today = new Date(); // create a Date object and put it into the variable today
document.write(today.toDateString() + " "); // convert to string the date represented by variable today
document.write(today.toTimeString()); // convert to string the time represented by variable today
document.write("<br>");
}
</script>
</head>
<body>
This is the body. <br>
<script>
printToday(); // calling a function
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> The Title of this HTML Page </title>
<script>
function printToday(){
var today = new Date(); // create a Date object and put it into the variable today
document.write(today.toDateString() + " "); // convert to string the date represented by variable today
document.write(today.toTimeString()); // convert to string the time represented by variable today
document.write("<br>");
}
</script>
</head>
<body>
This is the body. <br>
<input type="button" value="click me" onclick="printToday()" />
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> The Title of this HTML Page </title>
<script>
function printHello(){
alert("hello <br>");
}
</script>
</head>
<body>
<script>
printHello();
printHello();
printHello();
printHello();
</script>
</body>
</html>
window.alert(); document.write(); // Writes HTML or JavaScript code to a document prompt(); // Displays a dialog box that prompts the visitor for input decodeURI(); // Decodes a URI back(); // Loads the previous URL in the history list ceil(); // Returns x, rounded upwards to the nearest integer click(); // Simulates a mouse-click on an element getDate(); // Returns the day of the month (from 1-31) scrollBy(); // Scrolls the document by the specified number of pixels sqrt(); // Square root y = sqrt(x)
are all functions. You can identify a function/method by the use of parenthesis.
(See W3schools JS Reference for a list)
alert('hello world');
Arguments allow you to extend or change the behavior of functions by passing information to it.
function functionName(var1, var2, var3) {
// javascript code
}
functionName(argument1, argument2, argument3);
<!DOCTYPE html>
<html>
<head>
<title> The Title of this HTML Page </title>
<script>
function area(x, y){
alert(x * y);
}
</script>
</head>
<body>
<script>
var length = 3;
var width = 5;
area(length, width);
area(length, 10);
</script>
</body>
</html>
var answer = prompt("What is your name?");
var answer = "Reed";
function functionName(argument1, argument2) {
//java script code
return somevalue;
}
<!DOCTYPE html>
<html>
<head>
<title> The Title of this HTML Page </title>
<script>
function area(x, y){
var a;
a = x * y;
return a;
}
</script>
</head>
<body>
<script>
var b;
b = area(3, 10);
document.write(b);
</script>
</body>
<!DOCTYPE html>
<html>
<head>
<title> The Title of this HTML Page </title>
<script>
// name is a argument and can be used just a variable
var TAX= .08; // 8% sales tax
function calculateTotal(quantity, price){
var total = quantity * price * (1 + TAX);
return total;
}
</script>
</head>
<body>
<script>
var salesTotal = calculateTotal(3, 15);
document.write("Total price: $" + salesTotal + "<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> The Title of this HTML Page </title>
<script>
/* A function that generates random color found on the Internet */
function get_random_color() {
return "#" + (Math.round(Math.random() * 0XFFFFFF)).toString(16);
}
</script>
</head>
<body>
<script>
document.body.style.backgroundColor = get_random_color();
</script>
</body>
</html>
var sayHello = function(){ alert("Hello"); };