So now it's time to learn about the difference between a global and the local variable. So the difference is that a global variable is known throughout our program, which means that each function knows exactly what this variable is. But a local variable on the other hand, only one code, or one piece of code, or one function, or one part knows about it. So it's a local variable. It's locally known for a specific part of our code. So let's now take an example.
So suppose that now we have a function called print number. And what this function is going to have inside a variable called called a and it equals to one right I'm going to just use the console dot log, and I am going to print that number here, right. And now if I call this function print number, what I'm going to get is obviously one, because we told this function to print that number, that variable here, right? But now suppose that we want to access this variable a, if I try to do so here outside, I'm going to delete this or comment this out. And here I'm going to use the console log dot log. And I try to just print this variable a, if I save and run, what I'm going to get is an error.
And why is that error? Because this variable is only known throughout this function, which means that this variable is only known inside These two curly braces outside these two curly braces, this, this variable is not known at all. And here is going to tell you a is not defined, because it is not known. It's unknown variable outside the function. But suppose that on the other hand, suppose that we delete this, I'm gonna delete this. And instead of defining this variable, a inside print number, if I define it, let me copy this and define it outside this function.
Let me define it here. And let me again call this function print number. If I save and run, what I'm going to get is one. Why is that? Because now this variable is known inside and outside this function. So as long as we are inside our file, it's known and I'm going to prove That with another function.
So suppose that we have another function that is called show number, show number. And this variable, this function is going to do the same exact thing, just console dot log. And if now I call this function show number. If I save and run, what I'm going to get is also one, which means that a is known inside these two functions and outside these two functions as well, so it is known throughout our file. But if the variable is defined inside the function, it will only be defined it will be only be defined and known inside that function. And it's going to be called a local variable.
But if it's defined outside the function here in the Indigo Bell area, we call it a global variable. And moreover, we can even change the value of that number from any of these functions. So here I can use a equals to two, right? If I save and run, I'm going to get to because a now has been changed by this function, because it's a global variable, and it can be accessed from any function through it can be accessed from any function inside this file, and it can be changed by any function. So it's very important to know the difference between a global and the local variable and we're to access them