So basically in a tiny nutshell: Variables throughout the entire script are essentially collected together whenever they are declared (whether they are "assigned" at the time or not) at the top, so the script knows they are available to work with and won't throw an early error. But they won't have value(s) until the line they are officially assigned them in. Function expressions, because they use variable assignment, can't be called early. Why do so many other videos have to complicate this concept? Yours was the first that actually managed to teach it so straightforwardly.
Great clarification at the end! I like how you keep it short, sharp and cover all the knowledge required without having to bring in unnecessary terms that may confuse people who may not require a deeper theoretical understanding of the ES specs - such as the execution context, etc.
4:00 it seems like the javascript engine can only know that a variable exists only if you declare the variable, but once you comment it out, you get an error. If javascript gives an error when the declared variable is commented out, how can you say it only knows that the variable exists if it has been declared? It needs it uncommented out for it to know it exists, it seems.
The declaration of a variable establishes its scope. If a variable has not been declared it is placed in the global scope once the JavaScript engine encounters it. So it knows about it once it encounters it, but then needs to give it a scope, which becomes global.
There are certain situations where a function expression is necessary. But when you are simply declaring them, you can use either one. You may want to look at the tutorial on function declarations and function expressions: th-cam.com/video/gjLn95skIKE/w-d-xo.html
Great explaining, learning a lot from your videos! Can you please take a look at this example and tell me why example 2 doesn't print '4, 5' ? me and my colleagues at work have been trying to figure out, maybe link me a video of yours that explains why: jsfiddle.net/p1qnt6bL/
It has to do with scope. Even though arr1 and arr2 in function test2 are named the same as arr1 and arr2 outside the function, they are not the same variable. The scope for the variables in test2 is only test2. So if you console log outside of test2, you get the variable that is outside test2. Place a console.log statement inside test2 and you will see the [4, 5]. I hope that helps.
@@AllThingsJavaScript I understand they are not the same variable, but arrays in javascript are objects right, so if I pass an array as a parameter shouldn't it be basically the same? (both should point to the same object) like in the example 1 I push a new value to the array inside the function and the array outside the function gets the new value also. Thank you for the response.
So what I believe is going on here is basically what I described before. You are correct that objects (arrays) are mutable. So they can be changed. But in test2 we are not mutating the array like is being done in test. We are trying to reassign the variable to a new array. The reassignment takes place inside the function, but doesn't exist outside the function because of scope. Does that help?
@@AllThingsJavaScript Yes that is actually the reason. If we reassign, different scopes won't see the change, just tested with an Object instead of an array and got the same result. Thanks!
So basically in a tiny nutshell: Variables throughout the entire script are essentially collected together whenever they are declared (whether they are "assigned" at the time or not) at the top, so the script knows they are available to work with and won't throw an early error. But they won't have value(s) until the line they are officially assigned them in. Function expressions, because they use variable assignment, can't be called early.
Why do so many other videos have to complicate this concept? Yours was the first that actually managed to teach it so straightforwardly.
Glad it was helpful!
Great clarification at the end! I like how you keep it short, sharp and cover all the knowledge required without having to bring in unnecessary terms that may confuse people who may not require a deeper theoretical understanding of the ES specs - such as the execution context, etc.
Thanks! Glad it was helpful.
4:00 it seems like the javascript engine can only know that a variable exists only if you declare the variable, but once you comment it out, you get an error. If javascript gives an error when the declared variable is commented out, how can you say it only knows that the variable exists if it has been declared? It needs it uncommented out for it to know it exists, it seems.
The declaration of a variable establishes its scope. If a variable has not been declared it is placed in the global scope once the JavaScript engine encounters it. So it knows about it once it encounters it, but then needs to give it a scope, which becomes global.
very interesting and helpful
So is it either Fun Exp or Fun Dec? Or are there some situations when one would be better than the other or can you write the same code either way?
There are certain situations where a function expression is necessary. But when you are simply declaring them, you can use either one. You may want to look at the tutorial on function declarations and function expressions: th-cam.com/video/gjLn95skIKE/w-d-xo.html
Okay perfect, thanks for the reply.
All right. March 2020. Still confusing a bit, but I will work on it.
Does hoisting concept even still matter with let and const. They'd throw an error if you try to use them before they are declared
Variables declared with let and const are not hoisted. var declarations, function declarations and classes are hoisted.
great explanation sir
very helpful. thank you
Thank you for this video! I really like it
Best teacher.
helpful
I am sorry, hoisting is not working like this.it works totally different under the hood.
Add a description of how it works under the hood. It would be helpful to others. I'm simply describing how it works practically.
Who knows that LET and CONST will not hoist :P
Great explaining, learning a lot from your videos! Can you please take a look at this example and tell me why example 2 doesn't print '4, 5' ? me and my colleagues at work have been trying to figure out, maybe link me a video of yours that explains why: jsfiddle.net/p1qnt6bL/
It has to do with scope. Even though arr1 and arr2 in function test2 are named the same as arr1 and arr2 outside the function, they are not the same variable. The scope for the variables in test2 is only test2. So if you console log outside of test2, you get the variable that is outside test2. Place a console.log statement inside test2 and you will see the [4, 5]. I hope that helps.
@@AllThingsJavaScript I understand they are not the same variable, but arrays in javascript are objects right, so if I pass an array as a parameter shouldn't it be basically the same? (both should point to the same object) like in the example 1 I push a new value to the array inside the function and the array outside the function gets the new value also. Thank you for the response.
@@FelipeFerraz08 You are correct. I didn't look close enough. This is an interesting problem I need to look into.
So what I believe is going on here is basically what I described before. You are correct that objects (arrays) are mutable. So they can be changed. But in test2 we are not mutating the array like is being done in test. We are trying to reassign the variable to a new array. The reassignment takes place inside the function, but doesn't exist outside the function because of scope. Does that help?
@@AllThingsJavaScript Yes that is actually the reason. If we reassign, different scopes won't see the change, just tested with an Object instead of an array and got the same result. Thanks!