Differences Between Var, Let, and Const

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what everyone my name is Kyle and this is what death simplified we make the web easy to understand and accessible for everyone now you guys have all probably watched quite a few tutorial videos in JavaScript and you may have noticed that people declare their variables using var and let but don't really explain the difference between the two you may also even see people using comps to declare their variables and it could get confusing to determine which one is the correct one to use in different situations in this video I'm gonna go over the differences between var let and Const and at the end of the video I'm going to tell you which one you should use for which situations so let's get started now so if you didn't already know JavaScript used to only have one key word to declare variables and that was the VAR keyword this keyword allowed us to create variables which were function scoped but people started to run into problems with having these function scoped variables along with a few other caveats related to var so the let and Const keyword x' were created to create variables that are block scoped instead of function scoped and essentially the difference between the two is a function scope variable is available anywhere inside of the function that is declared in while a block scoped variable is only available inside of the block is declared in so for example an if statement or a loop would be a block that that variable is only available inside of but not outside of that if or loop for example so let's jump into an example of me showing you that now so I just have a simple example open on the left here where I have two different blocks to find with these if statements and inside of these blocks I'm declaring a variable and the first one I'm declaring using var and the second variable I'm declaring using let and then just logging the output of these variables to the console on the right side of the screen here so if we save this you'll see that this VAR variable returns this is true and that's perfect and it's using doing that because this VAR variable is function scoped and if there's no function that it's inside of issues globally scoped so it's available outside of this block of the if statement so that when we go to this console walk here it's able to log the value but when we get to this let variable we simply get a error over here saying that it can't actually find this left variable and that's because the web variable is only defined within the block that you created which means that inside of this if statement is the only place this let variables available but it's not available outside the if statement if we put this log inside the if statement here and we say that you now see that it's logging in sedative statement but outside of the if statement is still throwing that error because it's outside of the block that we've defined it in this works exactly the same if we define this using coms as you can see we get the exact same error because constant let are both block scoped while var is functions code another thing that's unique to var is that VAR variables can be reassigned multiple times by actually read eclair the variable so here we have far var variable equals this is true if I wanted to go maybe outside this if I say var var variable equals this is false for example it'll completely redefine this var variable but if I did this with what for example so if I did let let variable equals true and then right after that I tried to set it to false here if I say that you see that I see that let variable has already been declared I can't actually read a cleared Bulevar I can read it Claire var as many times as I want this is actually one of the things that I think is the biggest problems with the VAR keyword for declaring variables because it allows you to overwrite variables accidentally that you didn't actually mean to overwrite because normally when you declare a variable with a keyword in front of the databar let or Const you're thinking this is the first time this variable is being declared so if you are able to overwrite variables by doing this you may accidentally overwrite variables that you don't intend to which is the biggest problem with far and let takes care of that problem because it won't let us do that ever which will save us from making those simple mistakes another way that foreign but differ is that var actually allows us to use a variable before we declare it so for example if we have a variable we're just going to call it the far variable again and if we just wanna console.log bar variable and this is before we've actually declared bart variable you'll see if we run that we get an error saying that it's not defined but if we define this VAR variable afterwards and we run this you see that it actually runs as undefined instead of an error saying that the variable is not defined and that's because the VAR keyword allows us to create a variable after we use it and it still allows us to go through and say that that variable exists and now if we want to walk the scepter here you'll see that it prints true with the actual value reset it to here but let does not work this way if we change this to be let instead of bar and we save it you see that we didn't reference error saying that right here this variable is not actually able to be found because it's not created like it would be with var because var creates the variable even if it's declared after it's used it would just be undefined until it gets to the point where it's actually created if you're using var and Const works exactly the same way constant let are essentially exactly the same in every single way except for constants does not allow you to read Eclair the variable so for example if we have constant here I call this constant art and we would want to set it to the value of one weeks put a let in here call let bar will also set it to 1 and then let's say we wanted to change comps bar to equal to and we want to do the same thing with wet bar we're gonna change the let bar to be 2 if you save that you see that we get an error saying that assignment the constant variable is not allowed and that's because we can't change comstar to be a different value that's essentially the only thing that cost prevents us from doing and it's exactly the same as let otherwise but cons does allow us to actually change the properties of the object if it is an object so for example if we had it as an object instead and which had a name in here we're gonna say Bob for example and then we wanted a console.log the constant variable you see that we get Bob and if we come in here Const actually allows us to change the different properties in it so we could change the name to be Sally instead and if you say that you see that we don't actually get an error so Const only prevents us from changing the actual value of reassigning the variable to a different value but not actually changing the different properties in that value so in a way constant let are very similar so you can use them both and I would actually recommend you use Const over let unless you need to change the value of the variable and that way you make sure you don't accidentally change the value if you don't want to and that's essentially all the differences between Const var and let as we know var is at the function level so it is scoped much wider than the let and constant variables which are scoped at the block level which is much more like other programming language that you may have used before JavaScript because that's how almost every other programming language handles variables is by scoping them at the block level instead of the function level and because of this and many other reasons I think that you should use the Const and let almost every single time when declaring variables unless you have a very specific case for using far and even if you do I recommend you to highly think through why you want to use var because it's almost always better to change your design to use constant and let instead of using var and then as to use Const or let like I said earlier I would use Const for variables that don't actually change and I would default to using comps unless you need to change the variable such as the iterator inside of a loop and then use let in that case so I hope you guys learned something about the differences between Const var and let and when you should use the different ones in different scenarios if you guys enjoyed the video please make sure to leave a like down below and give me a comment let me know what other JavaScript topics you want me to cover in the future thank you guys very much for watching and have a good day
Info
Channel: Web Dev Simplified
Views: 48,476
Rating: 4.9773755 out of 5
Keywords: webdevsimplified, var vs let, var vs let in javascript, var vs let vs const, var vs let vs const javascript, should i use var or let, what is let in javascript, what is var in javascript, how to declare a variable in javascript, is var dead, is const immmutable javascript, var vs let explained, var vs let tutorial, diffrences between var and let, Differences Between Var let and const, differences between let and const, let vs const, let vs const vs var, let vs const javascript
Id: 9WIJQDvt4Us
Channel Id: undefined
Length: 8min 37sec (517 seconds)
Published: Sat Nov 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.