Javascript ES6 — var scoping # 1

lets understand how the var keyword works to create variables, so let’s start by creating a number variable using the keyword var that have a value of 10, accessing to the value of number after the declaration will display as a result the number 10, now if I take the same variable number and without redeclaring it I change its value to 20, now accessing the value of number before declaring it will still the same, however accessing to the same variable after changing its value will give me the updated value which it’s 20. from that, we assume that the variable number can update its values through the program, until now, noting is alerting.

let’s move forward little bit, for that, create a function setNumber and inside of this function add a new variable number which have a value of 30 then return the result, what I want to achieve using this function is I want to jail the variable number, which means that I want it to be accessible only within the function setNumber

The result of the function setNumber is 30, however, I have a question for you, can I access to the variable number outside of the function setNumber like that console.log(number)? pause the video and think a little bit, so the answer is NO, because this variable is only accessible within its scope, and just think of a scope like a box, so when you open and close bracket you’re jailing any element within its scope.

Now lets go further by trying scoping a variable within a condition instead of a function, okay, I’ve a variable var age = 17 and I’ll create a condition if the age is less than 18 if age < 18, open the bracket to create a scope, and inside of the condition, I’ll create another variable msg var msg = that include the message to show if the condition is true You’re not allowed to access.
So the condition is true, let’s console.log the variable msg within this condition, GOOD,

And I’ve another question for you, Can I access the variable message outside of the condition ?, the answer is YES console.log(msg), and it will give you the message, and I’ll foul you little bit, and even if try to access to the variable msg before the condition it’ll show Undefined, but what the hell is that, this is what you’re telling yourself right now, I know that you’re confused.

Just calm down, keep that in mind, the var keyword is used to jail variables within a function and not within a condition and it’s used to create global variables, because when you create a variable inside the condition it’ll be created globally and can be accessible everywhere and this is not what do you want to achieve for this use case, in this situation the var keyword is not block scoped and the let keyword can take its place, let msg = "You’re not allowed to access." now the debugger gives you an error msg is not defined, which mean that you can’t access the msg variable outside of its scope, the condition.

so far you solve the problem using let, why even this var keyword exist? these are some use cases when you want to update the value of variable within you program without recreating it multiple times, so creating a global variable will be super useful, however in most cases you’ll find yourself using const and let, and this is what we’ll discover on the next video.

Leave a Reply

Your email address will not be published. Required fields are marked *