Friday, December 6, 2019

Learn how to implement JavaScript Literals with examples

Hello guys! morning, welcome to my new tutorial post on JavaScript. In my previous tutorial we discussed about how to to implement JavaScript Function() constructor, so lets move on to another exciting topic on JavaScript called JavaScript Function Literals.

JavaScript 1.2 introduces the concept of function literals which is another new way of defining functions. A function literal is an expression that defines an unnamed function.


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

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

But this name does not have any significance,  so it is not worthwhile.

You can also read our tutorial post on: JavaScript - Loop Control


Try the following example below to understand how a function literal is used.

          <script   type = "text/javascript">
                    var func = function(x, y)  {
                         return x*y
                    function  secondFunction()  {
                         var  result;
                         result = func(10, 10);

          <p>Click the following button to call the function</p>     

               <input  type = "button"  onclick = "secondFunction()"  value = "Click">     



Below is the output of the above example

click the following button to call the function  


Alright guys we have come to the end of this tutorial post on JavaScript function literals. In my next tutorial we will be discussing about JavaScript Events.

Always feel free to ask your questions via the comment box below and don't forget to follow us on our various social media platforms, thanks and bye for now.


Post a Comment