0

Good day all,

This is not a specific question, but a general one.

In particular, i am preparing for a Javascript related interview. On Glassdoor, I saw someone mention that previously, "What is Javascript hoisting?" was a question that was asked.

In essence, I wanted to know what type material I should study on Javascript to know it to that depth.

Now, ideally, you would tell me to read a book and learn as much about Javascript such as if I were to be asked what hoisting was, that I would be able to answer it "naturally."

However, this raised an important thought with me, in that I know I have a limited amount of hours, and being able to study "smart" for interviews would be a good skill for future interviews, that may not necessarily test Javascript knowledge.

Is hoisting considered a "feature" of Javascript? What are such phenomena called, and are there books that specifically highlight the key features of a language that distinguish it from most others.

This is a very vague questions, but in essence I want to find out a way where I can learn about in-depth features of a programming language without having to study from square one, up to that point.

Hope that made sense!

Best regards, Andy

Andy Liao
  • 15
  • 4

1 Answers1

1

I guess you could call it a feature. Back in the day, you needed to make sure you defined every function before it was called, and everything would be compiled in the order it was written: Think about something like var myCat = new Cat(); function Cat() { ... }, the code would throw an error at the new Cat() token (something like undefined is not a constructor), because, at the time new Cat() is executed, the Cat function is never defined. It's defined later in the code.

With hoisting, variable declarations are loaded into memory at compile time, and you can initialize and use variables/functions before they're declared, meaning you don't have to worry about this conflict. That's really all there is to it. It's effectively a compile-time optimization - something to make peoples' lives easier, like how for (var i = 0; i < array.length; i++) array iterations are largely deprecated (outside of closure compiled code etc.) because of the Array.prototype.forEach builtin.

You can read more about function hoisting on MDN's Web Docs.

// call to catName before declaration
catName("Chloe");

function catName(name) {
  console.log(`My cat's name is ${name}!`);
}
Lewis
  • 4,285
  • 1
  • 23
  • 36
  • Everything is still compiled in the order it's written. – Dave Newton Mar 06 '20 at 01:47
  • @DaveNewton Not sure I'd say that a multi-pass compiler respects any "order". – Bergi Mar 06 '20 at 02:27
  • @Bergi Implementation-dependent; generally handled during linking phase anyway--as much as there is a linking phase, not sure I'd call it that in something like JS but end result is the same. – Dave Newton Mar 06 '20 at 02:31
  • Hmm, that makes a lot of sense. Then, how is the scope of the hoisted variables kept in check? Is there a "scope tag" that is brought together, into memory, with the hoisted variable/function? Thanks. – Andy Liao Mar 06 '20 at 02:44
  • 1
    An answer to my comment (to Herculean's post) from either of you would be nice :) I think it's an important question for me to really understand what's really going on when the code is being run. Thanks! – Andy Liao Mar 06 '20 at 03:06
  • @AndyLiao, I really don't know much past what I mentioned - I did find [this blog post](https://hackernoon.com/scope-hoisting-in-javascript-19b991babc4f) that goes over scope and hoisting, but when it comes down to JS compilation, I'm in over my head! – Lewis Mar 06 '20 at 03:48
  • Thanks!! Regardless, I don't think that they won't go that in-depth anyways. – Andy Liao Mar 06 '20 at 04:08
  • @AndyLiao https://stackoverflow.com/a/32475965/1048572 – Bergi Mar 06 '20 at 09:00