top of page

JAVASCRIPT IS A BRO AT HEART ("Bro, do you even hoist?")

  • Winnie Au
  • Aug 18, 2015
  • 2 min read

Even though it was designed in 10 days by Brendan Eich (we imagine in the depths of his basement), JavaScript is really a bro at heart. Why? Because JavaScript hoists. All day, errrrday.

What is hoisting though? Hoisting, in the world of Javascript means that variables are "hoisted" to the top of their containing scope. But what does that mean? First let's talk about scope. Scope is like the context in which something exists and has meaning. The scope specifies from where you can access, for instance, a variable and whether you have access to that variable in that context. JavaScript has "function-level" scope. This means that variables declared within a function are local and can only be accessed within that function or by functions inside that function.

Let's revisit what was mentioned earlier. Hoisting, essentially means that variables declared anywhere within a function's scope are going to be brought to the very top automatically. So even if you haven't previously mentioned a variable, if you declare it later, JavaScript will be okay with it because of hoisting. This doesn't mean, however, that you can actually use the variable as you intend to if you haven't initialized it yet.

Initialization and declaration are two different things. Initialization is when a value is applied to a variable, whereas declaration is when the variable is declared. Makes sense. But be careful because not acknowledging these JavaScript quirks can get you in some trouble (namely vague errors or bugs that could have been easily avoided).

To make this concept more clear, take a look at the first paragraph of this blog. Did you notice how I used the word "it" before I told you what "it" was? That's kind of like how JavaScript works. If you declare "it" (a variable essentially) somewhere that's not at the top of your code, JavaScript won't shun you for saying "it". But until you tell JavaScript what "it" is or means, you will likely get an "undefined" error while running tests if you try to call "it".

To wrap things up, you don't *have* to put all your variables and functions at the top of your code if you don't want to because JavaScript will hoist those babies up there for you, but you should because it's good practice and can save you from potentially mind boggling errors later. Also, remember to assign values to those variables before give them a shout, because like a bunch of people waiting at the airport with blank signs, they won't really do anything for you otherwise.

Below you will find some links to web pages that I found very helpful and gathered the bulk of this information from.

?xml version="1.0" encoding="UTF-8" standalone="no"?http://code.tutsplus.com/tutorials/javascript-hoisting-explained--net-15092

?xml version="1.0" encoding="UTF-8" standalone="no"?http://www.sitepoint.com/back-to-basics-javascript-hoisting/

?xml version="1.0" encoding="UTF-8" standalone="no"?http://www.w3schools.com/js/js_hoisting.asp


 
 
 

Comments


whoami

Aside from blogging and coding, I love eating, cooking, traveling, playing string instruments and spending time with my fiance and our dogs. 

 

See the "About" section for more info!

Other Posts
Follow Me
  • Facebook Basic Black
  • Twitter Basic Black
  • YouTube Basic Black
Search By Tags

© 2023 by BI World. Proudly created with Wix.com

  • Facebook Basic Black
  • Twitter Basic Black
  • YouTube Basic Black
bottom of page