Copyright © tutorialspoint.com

JavaScript Function Literals

previous next

Advertisements


JavaScript 1.2 introduces the concept of function literals which is one more new way of defining functions.

A function literal is an expression that defines an unnamed function.

Syntax:

The syntax for a function literal is much like that of the function statement, except that it is used as an expression rather than as a statement and no function name is required.

<script type="text/javascript">
<!--
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>

But this name does not have any significance, so worth not to use it.

Example:

Here is an example of creating a function in this way:

<script type="text/javascript">
<!--
var func = function(x,y){ return x*y };
//-->
</script>

You can call above function as follows:

<script type="text/javascript">
<!--
func(10,20); // This will produce 200
//-->
</script>

To understand it in better way you can Try it yourself.


previous next

Copyright © tutorialspoint.com