Why JavaScript allows you to place functions anywhere in your file

We know that JavaScript doesn’t care if your functions are declared before or after you use them! We see that when we execute any JavaScript script and I ask myself a BIG WHY! I found it and I’m glad to share it with you.

 

photo-book1

This situations might seem really rare, especially if you remember when the browser loads your page, it starts executing JavaScript from the top to the bottom on your file. But that’s an ANSWER for my WHY:

JavaScript makes two passes over your page:

  1. First, It reads all the function definitions,
  2. Second, It begins executing your code

So that’s WHY JavaScript allows you to place functions anywhere in your file!

Take a look at code example on my gist.

Stay Strong,

ddeveloperr

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

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