#
JavaScript Functions
This tutorial explains to you how you can work with JavaScript functions. JavaScript functions are very used in web development.
Here is a code I have tested for showing how the JavaScript function works:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
var add100 = function(x) {
return x+100;
}
var add1000 = function(x) {
return x+1000;
}
function doSomething(var1, function1) {
if (function1 === undefined)
return var1;
else
return function1(var1);
}
function add200(x) {
return x+200;
}
var x1 = doSomething(1);
alert("x1= "+ x1);
var x2 = doSomething(10, add100 );
alert("x2= "+ x2);
var x3 = doSomething(1000, add1000);
alert("x3= "+ x3);
var x4 = add200(x1)
alert("x4= "+ x4);
</script>
</body>
</html>
And here are the results of the code which use JavaScript functions:
Info
- add200 is a normal function which is defined and used;
- you can define a variable as a function and used when you call another function in JavaScript;
- a function can have null arguments. You can check this using "undefined".