Functions as Value in JavaScript

JavaScript Functions

  • Can be defined
  • Can be invoked

Apart from this JavaScript function can be treated as value as well. JavaScript functions act as Value. They can

  • JavaScript function can be assigned to a variable
  • JavaScript function can set as property of an Object
  • JavaScript function can be passed as argument to other function
  • JavaScript function can be returned from function etc…

Let us say there is function as below,

function FindGrade(e) {

if (e > 60)
 return "Grade A"
 return "Grade B"

You can assign this function to a variable,

var abc = FindGrade;
 var result1 = FindGrade(20);
 var result2 = abc(70);

You can assign function as Object property also. This can be done as following,

var obj =
 grade : FindGrade
 var result = obj.grade(70);


You see that above we assigned a JavaScript function as Object’s Property. You can use JavaScript function as argument of another function etc. I hope you find this post useful. Thanks for reading.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s