Placeholder Image

字幕表 動画を再生する

  • Hello, everyone.

  • My name is Kyle and this is what does simplified.

  • We make the Web easy to understand and accessible for everyone.

  • Now you guys have all probably watched quite a few tutorial videos and JavaScript, and you may have noticed that people declare their variables using far and wet.

  • But don't really explain the difference between the two.

  • You may also even see people using comes to declare their variables, and it can 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 bar let and a constant, 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, Java script used to only have one key word to declare variables, and that was the bar keyword.

  • This keyboard allowed us to create variables which were function scoped, but people started to run into problems with having these functions.

  • Scope variables, along with a few other caveats, related too far, so the let and constitute words were created to create variables that are blocks 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 blocks cooked variable is only available inside of the block is declared it so.

  • For example, if statement or loop would be a block that that variables only available inside of but not outside of that if or Luke, for example.

  • So let's jump into an example of me showing you that now.

  • So I just have a simple example open on 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 1st 1 I'm declaring using bar and the second variable I'm declaring using.

  • Let I meant just logging the output of these variables to the council on the right side of the screen here.

  • So if we save this, you'll see that this bar variable returns.

  • This is true, and that's perfect.

  • And it's using doing that because this far variable is function scoped.

  • And if there's no function that is inside of issues globally scoped so it's available outside of this block of the if statement so that when we go to this council walk here, it's able to log the value.

  • But when we get to this, let bearable.

  • We see we get a hair over here saying that it can't actually find this left variable.

  • And that's because the left variable is on Lee defined within the block that you create it, which means that inside of this, if statement is the only place that's let variables available.

  • But it's not available outside if statement.

  • If we put this log inside the of statement here and we say that you now see that it's logging inside of the statement, but outside of that statement is still throwing that error because it's outside of the park that we would find it in.

  • This works exactly the same if we don't find the shoes in Constant.

  • As you can see, we get the exact same air because Constand let are both block scoped, while far is function scoped.

  • Another thing that's unique tow bar is that bar variables can be reassigned multiple times by actually re declaring the variable.

  • So here we have far, far variable equals.

  • This is true if I wanted to go.

  • Maybe outside is if I save our bar Variable equals This is false.

  • For example, it'll completely redefined this var variable.

  • But if I did this with let, for example So if I did let's wet, very bold 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 wet variable has already been declared.

  • I can't actually re declare it, but, levar, I can read it.

  • Clear the bar as many times as I want.

  • This is actually one of the things that I think is the biggest problems with the bar key word for declaring variables because it allows you to override variables accidentally that you didn't actually mean to overwrite because normally when you declare variable with a key word in front of the date of our let her constant, 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 override 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 bar actually allows us to use a variable before we declare it So, for example, if we have a variable would call it far variable again if we just wanna council dot want bar variable and this is before we'd actually declared bar variable.

  • We'll see if we run that we get an error saying that it's not to find.

  • But if we do find this of our variable afterwards and we run this, you see that it actually runs his undefined instead of an air.

  • Same of the variables 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 exist.

  • Now if we want to launch this after here, you'll see that it prints true with the actual value he said it to hear.

  • But let does not work this way.

  • If we change this to be let instead of art, we save it.

  • You see that?

  • We get a reference air saying that right here this variable is not actually able to be found because it's not created like it would be with bar because of our creates the variable.

  • Even if it's declared after it's used, it will just be undefined until it gets to the point where it's actually created.

  • If you're using a bar and constant works exactly the same way, Constant left are essentially exactly the same in every single way, except for Constance, not allow you to re declare the variable.

  • So, for example, if we have constant here, call this constant bar, we wouldn't want to set it to the value of one.

  • Please put a wet in here called Wet Bar will also set it to one.

  • And then let's say we wanted to change comes far too equal to, and we wanted the same thing with wet bar.

  • We're gonna change.

  • Let bar to be, too if you say that you see that you get an air saying that assignment to the constant variable is not allowed, and that's because we can't change.

  • Constant are 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 Khan's 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, just a Bob, for example, we wanted a consul dot log the constant variable.

  • You see that we get Bob, And if we come in here, Constant actually allows us to change the different properties in its We could change the name to be sadly instead.

  • And if you say that, you see that we don't actually get an air so constant Onley 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 constantly are very similar, so you can use them both.

  • And I would actually recommend you use constant 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 constant bar and let, as we know far, is at the function level.

  • So it is scoped much wider than the let and constant variables, which are scoped out the block level, which is much more like other programming language that you may have used before Java script.

  • Because that's how almost every other programming language handles variables is by scooping them at the block local instead of the function level.

  • And because of this and many other reasons, I think that you should use constant 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 highly think through why you want to use far, because it's almost always better to change your design.

  • To use Constand let instead of using bar and then as to use ConStor let.

  • Like I said earlier, I would use constant for variables that don't actually change.

  • That would default to use incomes unless you need to change the variables such as the it aerator inside of a loop, and then use let in that case.

  • So I hope you guys learn something about the differences between constant bar and let and when you should use the different ones in different scenarios.

  • If you guys enjoy the view, Please make sure to leave it like down below and give me a comment.

  • Let me know what other job script topics you a minute cover in the future.

  • Thank you guys very much for watching and have a good day.

Hello, everyone.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

Var, Let, Const の違い (Differences Between Var, Let, and Const)

  • 12 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語