Hello, in this JavaScript video, I am going to cover the block scope. So depending on where your variable is actually declared, not set necessarily with a value but declared, it will depend on where it can be accessed and what value it has when accessing it. So, first of all, if I create a simple function called function, and let me just call a second phone. If I create a variable, epic num equals equals 789 doesn't really matter what the value is. It could be a string could be Boolean, be anything really. Console dot log.
And I try and print out epic. Let's see what happens. Yes, seven, eight. No, you know as expected So copy this. Down here, we get an error, it says epic num is not fine. And in my thinking we've defined it here, that's true.
But as soon as that you have these curly braces, the declaration, so anything that you know has more or less at the start, and then a variable name, so the declaration line is restricted within those curly braces. So this does not exist inside of these curly braces. Another thing, you can actually just put curly braces, I want to say for the sake of it, but just to limit scope within a limited scope. Already, if I were to put some curly braces here, again, this will just execute as soon as the function is executed. Once it's done this line, this line is executed because it's not tied to a conditional statement or a function, but it will allow you to limit scope. So if I were to do you could do console dot log Unless for the console dot log e cool.
Epic now let's see what we get. So yeah 789789 what happens if we do var or less? There is just a little bit better because he took pro extra error. They just helped you work better quality code instead. And now we get 800. But what happens if I do console dot log outside of this function and our print line or epic num?
We get 789 more thinking but we assigned 100 here, yes, but we actually created a normal variable within this scope. And as a result, this is actually a whole nother variable. And that's just something to bear in mind. If we What you do is like this, however, you'll print 100, because by creating a new one, it's just a sign to the previous one. So that's just, you know, scope in terms of JavaScript. They're similar scope in most programming languages is a very powerful feature that allows you to create variables and limit the usage of it to prevent any, you know, accidental usage, where it could cause some harm and lead to crashes potentially.
So that's it. Thanks for watching. If you have any questions, feel free to shoot me a message and I look forward to seeing you in the next JavaScript video.