var, let and const for beginners (ES6)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up my name is Jay and in this quick video I want to show you the differences in how you can use var let and cost now lettin const are part of es6 if you want to learn more about es6 there's a link on the description of this video alright so here I have you can see a bar so it's a variable called cats is equal to 2 and I have here a very simple function name my cats and you can see like if says if cats is less than 5 then console.log dis and then if it's not then comes a lock all this and you can see here cats is less than 5 because it's 2 and it's console logging I have two cats because I have here I have and then whatever is inside that variable cats simple alright so first we're gonna start by saying var is function scope or global scope by global scope I mean something like this right you can just declare a variable outside functions or outside any code inside blocks and it's available for us for anything right so as you can see here I'm using cats here that's the variable but I told you that variable our function scope so we can do something like this we can really clear which is something that variables is gonna let us do cats equals to let's say 10 for example I'm gonna save this all right and now it changed to that's a lot of cats why because is function scope okay now we're using this variable and he's not getting out of this function now this function have his own cats variable so now cats is equal to ten instead of two all right so let's console.log here cats before I'm calling the function save it and you can see that is two why is that because right now this variable cats is inside a function so it's only available for this function is not getting out of the function cool so this is something that you need to be very careful because let's say that we have this if true let's say that whatever you have here is true let's say that we read Eclair the variable for some reason cats equal to the say fifteen now take a look at this now cats is 15 so this is what you need to be careful so let's say you have this variable and it's equal to 2 but then you change it you really clear it and now is 15 and maybe for some reasons like you're confused why cats now 15 and all that stuff so let's see what happens with let so let's change this var or let let's save it oh the identifier cats has already been declared if you're using let is not gonna let you read Eclair that variable you can update but you cannot read Eclair plus let is block scoped and when I say blood scoped is that inside curly brackets it's not getting out of there okay this is a block a block right here you can see curly bracket is a block this is another block this is another block so let's see that we change this var to a let and now we don't have an error why we don't have an error well because this is a block right here take a look we have curly brackets and this is a block now this let cats equal to 15 is not getting out of this if statement okay it's not gonna leak out so he's not gonna give us an error but remember these cats equal to 15 he's not available for us global scope like this one right here now you cannot read Eclair let variables but you can update them okay so I can update here instead of really clearing it I can say cats equal to 15 I can save it and now it's 15 because I'm updating these cats equals to 2 here so if it's true cats is now equal to 15 great so let's remove this and let's change this one to to Alette alright so remember the let is block scoped so this is a block curly brackets and Cali brackets great so now this cat is equal to ten let's change it to let's say four all right I have four cats perfect remember you cannot really clear so let's do let's do it now let's see what happens save it perfect I get the error but you can update it so let's remove the light so now you're updating instead of really clearing and there you go that's a lot of cats okay now let's add a let inside the if so let cats equals to one save that and now you can see we don't have errors I have one cats which is I need to remove that s but that's fine but why we don't have an error because we have a block so it's not leaking out of this block okay so now this cat is its own variable inside this little block of code right here alright so I hope that explains a little bit let now let's jump to cost so let's use Const here Const equals to two let's save it nothing happened and now let's say Const cats equals to three save it error has been declared all right so we cannot declare it again lest update it instead of declaring it save it good assignment to constant variable so you cannot update Const okay so constant is a constant variable is not gonna let you read eclair it's not gonna let you update so this is something that's gonna stay the same okay if it's an object you can update the properties inside that object so if you say Const I don't know obg equals two and then you have cat is black and then you less console.log obg dot cat see what this is so with black okay so I want to say obg dot cat let's do it up here obg cat equals two purple it updates a purple and this is a Const but you cannot update the whole thing so you cannot do something like this right like purple is gonna give you an error so you can update the property but you cannot update their object the whole thing okay so I hope this helps you a little bit understanding variables let and Const so at the end of the day these two are replacing VAR you will notice that from here now in my tutorials you will see me using Const or let I am not using var on that way you guys can do the same thing and practice and understand it better all right so that's it subscribe if you want to keep learning and click on the bell icon to receive notifications every time I upload a new video thank you so much for watching half a beautiful day bye bye
Info
Channel: iEatWebsites
Views: 6,547
Rating: 4.878788 out of 5
Keywords: var, let, const, const for beginners, let for beginners, is var dead, es6, es6 for beginners, new things in es6, how to use let, the difference between var let and const, vat let and cost for beginners, javascript beginners, javascript for beginners, javascript variables, javascript variables for beginners, es6 variables
Id: jwZv-TL3W90
Channel Id: undefined
Length: 9min 35sec (575 seconds)
Published: Mon Dec 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.