6th Week of #100DaysOfCode with JavaScript. What did I Learn so far?
Topic I learned this week: JavaScript Hoisting!
When I took a JavaScript course about two years ago in college, the topic of JavaScript hoisting didn't really get mentioned for some reason. It could be that I don’t remember but I know that this is a important topic to understand. I haven’t got a chance to really see how it works in a real project, but I now understand the basic concept of what it does. The simplest way that I can define hoisting is when variable and function declarations are moved to the top of their scope before code execution. So basically, no matter where the variables or functions are declared in the code, they will be moved to the top of their scope. Lets take a look at a code snippet to see how this works. I will first show a code snippet of how you would normally declare a function and then another snippet of how it works with hoisting.
In the example above, I just declared a function as I normally would without using any type of hoisting. Lets see what happens when I put the function call above the function declaration.
In this code example, I called the function before I even declared it and I still got the same result. This still works because of how context execution works in JavaScript. It is important to note that JavaScript hoist declarations and not initialization. Lets look at how this works.
The console will return undefined because only the declaration was hoisted and not the initialization. I am still new to this but this is the general understanding of how hoisting works.
Summary
Overall, I feel like I learned a important concept in JavaScript. I feel like I will understand why certain things work when creating a project. I actually started working on a project to learn DOM manipulation which is another important concept. This is my favorite thing that I learned so far. The game will be a dice game. I will talk more about this next week but it is super exciting!