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"
 else
 return "Grade B"
 }

You can assign this function to a variable,


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

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


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

 

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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s