Javascript Hoisting

In JavaScript, Hoisting is the default behavior of moving all the declarations at the top of the scope before code execution. Basically, it gives us an advantage that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.
It allows us to call functions before even writing them in our code.

Note: JavaScript only hoists declarations, not the initialisations.

Example1: Function Hoisting
alert(Sum(5, 5)); // 10
function Sum(val1, val2){
  return val1 + val2;
}
Example2: Function Hoisting
Add(5, 5); // error
var Add = function Sum(val1, val2){
  return val1 + val2;
}
Example3: Function Hoisting
alert(UseMe);
var UseMe;
function UseMe(){
  alert("UseMe function called");
}

347 Replies to “Javascript Hoisting”

  1. It’s appropriate time to make some plans for the longer term and
    it is time to be happy. I’ve learn this put up and if I
    could I desire to suggest you few attention-grabbing issues or tips.
    Maybe you could write next articles relating to this article.
    I want to learn more issues about it!

  2. It is perfect time to make some plans for the future and it
    is time to be happy. I have read this post and if I could I want to suggest you few interesting things or advice.
    Maybe you could write next articles referring to this article.
    I want to read even more things about it!

  3. It is appropriate time to make some plans for the future and it is
    time to be happy. I’ve read this post and if I could I
    want to suggest you few interesting things or advice.
    Perhaps you could write next articles referring to this article.

    I wish to read even more things about it!

  4. I’ve been surfing online greater than 3 hours these days, yet I
    by no means found any attention-grabbing article like
    yours. It’s beautiful worth sufficient
    for me. In my view, if all site owners and bloggers made excellent content as you probably did, the web
    might be much more useful than ever before.

  5. I will right away snatch your rss as I can’t in finding your e-mail subscription link
    or newsletter service. Do you have any? Kindly permit me know in order that I
    may subscribe. Thanks.

  6. I’ve been surfing online more than three hours today,
    yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did,
    the net will be a lot more useful than ever before.

  7. I’ve been surfing online more than 3 hours these days, but I by no means discovered any fascinating article like
    yours. It’s beautiful value enough for me. In my opinion, if all webmasters and bloggers made excellent content material as you
    probably did, the web shall be a lot more useful than ever before.

  8. Thank you for the sensible critique. Me and my neighbor were just preparing to do a little research on this. We got a grab a book from our area library but I think I learned more clear from this post. I’m very glad to see such excellent information being shared freely out there.

  9. I will right away take hold of your rss feed as I can’t in finding your email subscription link or e-newsletter service.

    Do you’ve any? Please let me realize so
    that I could subscribe. Thanks.

  10. Hi there! I know this is somewhat off topic but I was wondering which
    blog platform are you using for this site? I’m getting fed up of WordPress because
    I’ve had issues with hackers and I’m looking at alternatives for another platform.
    I would be awesome if you could point me in the direction of a good platform.

  11. Hi would you mind letting me know which webhost you’re working with?
    I’ve loaded your blog in 3 different internet browsers and
    I must say this blog loads a lot quicker then most.

    Can you recommend a good hosting provider at a reasonable price?
    Cheers, I appreciate it!

  12. I have been surfing online more than 3 hours today, yet I never found any interesting article like yours.

    It is pretty worth enough for me. Personally, if all website owners and bloggers made
    good content as you did, the internet will be a lot more useful
    than ever before.

  13. It is the best time to make some plans for the future and it’s
    time to be happy. I’ve read this post and if I could I desire to suggest you some interesting things or
    tips. Perhaps you could write next articles referring to this article.
    I want to read even more things about it!

  14. Have you ever thought about writing
    an ebook or guest authoring on other blogs?

    I have a blog based upon on the same subjects you discuss and
    would love
    to have you share some stories/information. I know my audience
    would appreciate your work.
    If you are even remotely interested, feel free to send me an
    e-mail.

  15. Wow, awesome weblog structure! How long have you been blogging for? you made blogging look easy. The whole look of your web site is fantastic, as well as the content material!

Leave a Reply

Your email address will not be published.