Humayoun Kabir
Humayoun Kabir

Reputation: 275

How to Call This Anonymous Javascript Function?

 var avg = function() 
   {
     var sum = 0;
      for (var i = 0, j = arguments.length; i < j; i++) 
        {

           sum += arguments[i];
         }
      return sum / arguments.length;
   }

When I try to call this like:

var average = avg(2,3,5);
average; // It works fine;

But how do I call it without assigning to a variable? If anybody can give any suggestion it will be delightful..Thanks.

Upvotes: 1

Views: 96

Answers (4)

phant0m
phant0m

Reputation: 16905

You need to understand the concept of expressions.

Every expression as a whole represents one value. An expression can be made up of multiple subexpressions that are combined in some manner (for example with operators) to yield a new value.

For instance:

  • 3 is an expression (a literal, to be specific) that denotes the numeric value three.
  • 3 + 4 is an expression, made up of two literal expressions, that as a whole yields the value 7

When you assign a value to a variable – as in var average = – the right hand side of the =-operator needs to be an expression, i.e. something that yields a value.

As you have observed, average will have been assigned the value five. It thus follows, that avg(2, 3, 5) must itself be an expression that evaluated to the value 5.

average itself is an expression, denoting the current value of said variable.

The most important thing to take away from this is, that avg() is in no way connected to var average =. avg() stands on its own, you can just think it like an ordinary value such as 5 (there are other differences of course).

If you have understood the concept of expressions and values, it should be clear that if you can do

var average = avg(2,3,5);
average; // It works fine;

You can also do

avg(2,3,5);

Upvotes: 0

Frederik.L
Frederik.L

Reputation: 5620

If you don't put the result into a variable or in a compatible context, this function cannot output anything, which makes it difficult to use unless you make it output the result. Try this :

var avg = function() 
   {
     var sum = 0;
      for (var i = 0, j = arguments.length; i < j; i++) 
        {

           sum += arguments[i];
         }

      var retvalue = sum / arguments.length;
      consoloe.log("avg: "+retvalue);
      return retvalue ;
   }

Then it may help you to see whenever the function is called or not.

Upvotes: 0

Guffa
Guffa

Reputation: 700352

You don't need to put the result from calling the function in a variable, you can do whatever you like with it.

For example, use it as the value in an alert:

alert(avg(2,3,5));

You can use it in another expression:

var message = "The average is " + avg(2,3,5);

You can use it directly in another call:

someFunction(avg(2,3,5));

You can even throw the result away by not doing anything with it, even if that's not useful in this specific situation:

avg(2,3,5);

Upvotes: 0

David G
David G

Reputation: 96810

You'd simply call it like this:

avg(2, 3, 5);

If you want to see the result, put it in an alert call:

alert( avg(2, 3, 5) );

Upvotes: 2

Related Questions