You can have nested functions in JavaScript. Nested functions means a function inside a function. So in JavaScript you can have a two functions defined as below,

image

Some important points about nested functions are as follows

  1. Nested function can access variable of parent function
  2. Parent function cannot access variable of nested function

Let us consider below example. In this you see that variable b is accessible inside nestedfunction.

function mainfunction(a, b) {

 alert(a);

 nestedfunction(88);
 function nestedfunction(c) {

alert(b);
 }
 }



Scope of nestedfunction is only to the function it is nested within. You cannot call nestedfunction from anywhere but the function it is nested within.

Nested function is very prominent in JavaScript. I hope you find this quick introduction of nested function useful.

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