15 JavaScript Tutorial | Scope of Variables | Global vs Local

Share this post on:

hey what’s going on guys thud Maya for telecoil earnings and in this quick short video to Doyle I want to discuss the topic of scope of variables so in a previous couple of videos of this entire playlist we’ve known what eyes functions in JavaScript we’ve also seen
what are variables so I thought let’s discuss the scope of variables and in JavaScript things are really easy we only have two different types of scope that are global and local so depending upon where you create the variable your variable is going to be having two different types of school and when I say scope it means that the life of the variable okay so let me just actually
show it to you so let’s say you create a variable K over your you assign the value of five and now you want to print it so you print it on the screen using the document dot write function and now what you can do is anywhere below in this entire script you can again copy/paste this and again print this variable and the value still exists right which means that this variable that you created over your is usable or is alive throughout this entire script till the very end okay so this is basically a global variable it means that its life is available throughout the entire page and throughout the entire script so what is a local variable then so when you define a
variable inside a function that variable is a local variable to that function okay so let’s try to do that now so we’ve already seen what our functions in the previous video, if you don’t know what functions are you, can check that video so I’m just going to create a
function saying function my function opening and bruising around brackets and the body of the function and now what I’m going to do is I’m just gonna cut this and paste it inside the function and now you can see the output we are not getting any kind of output because this a is now inside this function so now it is not a global variable it is a local variable which means that it exists only inside this function so
let’s cut this and let’s pH this inside and let’s give a function call to our newly created function so I’m going to say my function opening and closing round brackets and: and now you can see again files printed because we are giving off a call to this function these two lines inside this function is executed and variable exists only inside this function so it is local and that’s why it is printed
however, you can see we have one more document dot write which is outside the function but since our variable is declared and created inside the function your the value does not exist which means that the variable an outside. the function does not exist it is destroyed as soon as the function is over okay and this applies to the memory also so whenever you are running a JavaScript
and your programs your entire Chrome browser itself is in the RAM in it’s running in the memory so these variables are also created inside the memory so these local variables are created only till the function is executing and once the function is done executing the memory of that function is wiped out so the variables are also destroyed however the variables that are global which are available throughout the script will have a global lifetime which means they will exist throughout the entire running of the application as long as your tab is open okay so another thing about global variables is you can access global variables inside functions also so let’s say I create another global variable where B and IC value is 6 I can access this inside our own function also so if I say print B over here you can see if my function can access this B variable which is created outside of the function and you can see the values printed as 6 which means that global variables can be accessed inside functions however local variables which are inside the function cannot be accessed outside because here you can see we are doing that we are trying to print the local variable a but the value is not being printed ok so one last thing that I wanted to talk about variables which I did not mention in the first video of variables was when you create a variable we use var keyword right however in JavaScript we can exclude that it means that we do not
actually need to use them where keyword also and you can directly see where one is equal to and assign the value and this is still a variable, okay so you do not have to explicitly type in where but what happens is when you do this becomes a global variable by default okay so if I cut this and if I paste this inside no you are also this where one is still a global variable because we did not use the where keyword and now it is a global variable which means you can access the value outside of the function but there
is a slight issue and that issue is our new browsers operate under a strict mode wherein they restrict the use of such kind of global variables okay
so global variables which are created inside a function without using the where the keyword is not directly accessible even though they are global okay so I don’t want to get into a lot of details I just wanted to talk about that you can create variables without the where keyword and still you won’t get an error and then there is a slight difference between creating global variables inside the
function and outside the function I will drop some info in the video description about this strict mode of browsers you
can read on it but right now we don’t need it I just wanted to mention it so yeah this was a little bit about the scope
of variables in JavaScript now that you’ve seen the functions and variables the scope of the variable should be very clear by now if you like this video please give it a thumbs up to do share it with your friends let me know in the comments how this video was and I’ll see
you guys in the next video peace

Share this post on:

Leave a Reply