SHORTEST JS Program 🔥window & this keyword | Namaste JavaScript Ep. 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
do you know what's the shortest javascript program here it is the empty file i know you must be thinking that akshay are you kidding me but no i am not you know even though this file is empty there is nothing to execute still javascript engine is doing a lot of things behind the scenes so let us now see what happens behind the scenes when you run this shorter javascript program that is the empty file okay so let's go to the source and put a debugger over here you know javascript engine will still do its work what will happen is suppose i execute let me run this javascript program what happened a global execution context is created and javascript engine still creates the global execution context and also sets up the memory space okay though there is nothing to set up but still it does a job okay and it also does something interesting so javascript engine also creates something known as window okay i'll show you so if you go in the console and try to write window you'll get something what is this what is this window so we didn't do that we didn't put any window inside our code how did it come up and if you see clearly this is like a big object with a lot of functions and methods right these functions and variables are created by javascript engine you know and into the global space you can access all these variables and functions anywhere in your javascript program this functionality is given as by javascript engine okay and just like this window javascript engine also creates a this keyword okay at this for us so if we do this we also get something right and in the global level at the global level this this points to the window object but wait what is window so you know window is actually a global object which is created along with the global execution context okay so whenever any javascript program is run a global object is created a global execution context is created and along with that execution context this variable is created okay we will be talking about this in a separate video altogether it's a whole different concept but for now let us talk a little bit more about this global object created right so this global object in this case in case of browsers it is known as window so let me tell you one thing so javascript is not just run on browsers right javascript is also run on servers javascript is running in a lot of other devices and places right it is not just the browser running it and wherever javascript is running there must be a javascript engine there okay just like in chrome it is v8 so it is known as v8 mozilla firefox has its own safari has its own internet explorer has its own engine right so all these javascript engines has a responsibility to create this global object okay in case of browsers it is known as window in case of node it is known as something else and wherever you run the javascript program it is different but there is always a global object created and even though our file is empty javascript engine will create this global object and at this global level or at the base level in the global execution context this this is equally equal to window object okay so if you try to do this is equal equal to window you will see that it is true so at the global level this is equal to window so understand this very carefully let me just recap whenever you create an execution context a this is created along with it even for the functional execution context and even for this global execution context and at global level that this points to the global object that is the window in case of browsers okay so did you understand these two things good so let me also show you by putting a debugger inside the code okay so if i put a debugger over here in the first line so what will happen let me run this code see a global execution context is created and this memory is created this is the global memory and here it creates like all these things for us automatically we didn't ask for it but javascript engine does it automatically okay now i'll tell you something even more interesting okay so if we try to put any variable or function in the global space so let me take a break and tell you that what this global space global space i am talking about constantly right i am talking about global space global space global space so this global space is nothing but any code you write inside javascript which is not inside a function okay so suppose if i created a where a is equals to 10 this is in global space if i created a function b okay this is inside global space but if i create any variable or function inside any function okay so suppose if we have a where x is equals to 10 over here so this x is not in global space otherwise everything you see on the top level is the global space so to make things simplify anything which is not inside any function is the global space that simple okay so whenever you create any variables or functions in the global space what happens see i'll show you something interesting if i refresh the page these variables and functions get attached to that global object okay that global object what was it it was windows right so let us just log that window once again so if we see this window clearly if we create these variables and functions see now a is present on this window b is present on this window x will not be there okay x won't be there there is no x over here right so this is the global space so whenever you create any variables or functions over here in the global space these get attached to this window and how do we access this we can just simply write a console.log window dot a to access a let us just run it see you got 10 and to make things easier we can just do a console.log of a also so if you don't put anything in front of a so suppose if you don't put window.a so it automatically assumes that you are referring to the global space so whenever we try to access any variable and function in our program and we don't put anything in front of it it assumes that it is in the global space okay so i will give you one more example so suppose if i try to do a console log of x over here so if i try to run what will happen what will be the output see x is not defined because if you don't put anything in front of it it assumes and find tries to find this x inside the global space itself inside that global object and you won't find x over there so it logs as not defined and one more thing i wanted to tell you see at this level at the global level this points to the window object right so if you do a this dot a it will again print out the same thing see it again logs 10 over here so window dot a a this dot a all are the same thing all are referring to the same place in the memory space so once again what is the shortest program in javascript yes this is the shortest program no code okay so that's all in this video but in the next video we'll be talking about undefined and the difference between undefined and not defined okay we'll be going a little more deep inside that so watch that video but before moving on to that video give this video a thumbs up like always and thank you for watching namaste javascript [Music] so [Music] you
Info
Channel: Akshay Saini
Views: 79,322
Rating: 4.9830461 out of 5
Keywords: Javascript course, Namaste javascript, akshay saini, akshay saini javascript, javascript tutorials, best javascript course, javascript video series, javascript fundamentals, namaste js, javascript core concepts, advanced javascript, Shortest javascript program, execution context in js, global execution context, js memory allocation, undefined in js, how js works, window object in js, this keyword in js, global object in js, global js code, shortest js code
Id: QCRpVw2KXf8
Channel Id: undefined
Length: 8min 33sec (513 seconds)
Published: Fri Oct 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.