Hello readers! welcome back to another section of my tutorial post on JavaScript. In my last tutorial guide we discussed about how to to implement Javascript Function constructor, so let us move on to another exciting topic called the Javascript Function Literals.
The JavaScript 1.2 introduces the concept of function literals which is another new way of defining functions. A JavaScript function literal is an expression that defines an unnamed function.
The JavaScript 1.2 introduces the concept of function literals which is another new way of defining functions. A JavaScript function literal is an expression that defines an unnamed function.
Syntax
The syntax for a function literal is much like a function statement, except that it is used as an expression rather than a statement and no function name is required.
You can also read our tutorial post on: How to implement JavaScript functions in your program
<script type = "text/javascript">
<!--
var variablename = function(Argument List) {
Function Body
};
//-->
</script>
<!--
var variablename = function(Argument List) {
Function Body
};
//-->
</script>
Syntactically, you can specify a function name while creating a literal function as follows:
<script type = "text/javascript">
<!--
var variablename = function FunctionName(Argument List) {
Function Body
};
//-->
</script>
<!--
var variablename = function FunctionName(Argument List) {
Function Body
};
//-->
</script>
But this name does not have any significance, so it is not useful.
You can also read our tutorial post on: JavaScript - Loop Control
Example
Try the following example below to understand how a function literal is used.
<html>
<head>
<script type = "text/javascript">
<!--
var func = function(x, y) {
return x*y
};
function secondFunction() {
var result;
result = func(10, 10);
document.write(result);
}
//-->
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "secondFunction()" value = "Click">
</form>
</body>
</html>
<head>
<script type = "text/javascript">
<!--
var func = function(x, y) {
return x*y
};
function secondFunction() {
var result;
result = func(10, 10);
document.write(result);
}
//-->
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "secondFunction()" value = "Click">
</form>
</body>
</html>
Output
Below is the output of the above example
click the following button to call the function
Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be studying about the Javascript Events.
Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.