Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          So then one of the most fundamental things in JavaScript is this notion of variables.

          And what variables do is give us a way to store a value like a name or a number or an email, and then
          
          use that value whenever we want to later on in the file.
          
          Now there's a few different ways we can make a variable.
          
          The first way to create one is by using the let keyword.
          
          So that would look something like this.
          
          We say let then the variable name, for example, age, and we set that equal to something.
          
          So I'm going to set it equal to 25.
          
          Then we end this with a semicolon.
          
          Okay, So you can think of this as saying let this variable called age be equal to the value of 25.
          
          Now this value is now stored in memory and we can access it later on using this variable name.
          
          So I could just come down here and I could say console, dot log, and then the age variable, which
          
          is what we called it.
          
          And that's going to log this to the console.
          
          So if I save and check it out in a browser, we see 25.
          
          Okay.
          
          All right then let's do another example.
          
          I'll say let year equal to 201 nine and we're going to save that.
          
          And we want to log this to the console as well.
          
          Now, I could duplicate this onto the next line, but in one console log statement, we can actually
          
          output several values, and we do that by separating them with a comma.
          
          So I could just say age comma, then year and that's going to output both of these values.
          
          So if I save now preview, we can see 25, then 2019.
          
          Okay, So this let keyword right here creates us a variable.
          
          And if we want to override the value of those variables later on, we can do so.
          
          I could come down here and I could say age is now equal to 30.
          
          And what that's going to do is look at where we initially defined age, grab that value and update it
          
          so that age is now equal to 30.
          
          We don't have to say let anymore.
          
          We just take the variable that's already been defined and change the value.
          
          So now if I say console, dot log and then age underneath, hopefully we should see 25 to begin with.
          
          Then we reset the value of the variable.
          
          Then this should be 30.
          
          So save it and preview and we see 25 to 019 and then 30.
          
          Cool.
          
          Okay then.
          
          So what if we wanted to create a variable but we don't want the value of that variable to be able to
          
          change?
          
          Well, in this case we'd use a different keyword than let we'd use the const keyword.
          
          So let's now do another variable, but this time use const and this creates us a constant.
          
          We still give this a name, so I'll call this points, for example, and set it equal to something I'll
          
          say 100 and this creates a variable much in the same way as the let keyword does, and we can log it
          
          to the console.
          
          So I'll say console, dot log and then points and we're still going to see that over here we can see
          
          100.
          
          But now if I try to override this value the same way we did with this, then it's not going to let me
          
          if I say points is now equal to something else like 50, save it.
          
          Then we get an error.
          
          Over here we see assignment to constant variable.
          
          We're not allowed to do that.
          
          We use a constant if we don't want this value to be overridden by mistake at any point.
          
          Okay.
          
          So something that's going to stay the same throughout the whole program.
          
          Now both of these keywords const and let.
          
          They are fairly new additions to the JavaScript language.
          
          They're the modern way to create variables and they're what I would recommend using almost 100% of the
          
          time.
          
          Now I will generally use Let if I think I might change the value of the variable at some point, or
          
          sometimes when I know that I don't want to change or overwrite the value of a variable, I'll use const
          
          instead.
          
          But there is another older keyword that we can use to create variables too, and that is the var keyword.
          
          Okay.
          
          So before let and const came about, this is what we'd use.
          
          Now I'm just going to do a quick demo and show you this.
          
          So I'm going to say var then name this variable score and set it equal to 75.
          
          Then I'm going to say console dot log score.
          
          So this should still work in exactly the same way.
          
          I'm going to get rid of this thing where I tried to override the value of points so we don't get the
          
          error, save it, and down here we can still see that score.
          
          Okay, 75.
          
          So this right here, this var keyword, this is the older way to create variables before let and const
          
          came about.
          
          Now you still will see some developers use this, but in this course, because we're doing a modern
          
          JavaScript course, I'm just going to be using const and let.
          
          Okay.
          
          I just wanted to let you know about that, that older way of creating them.
          
          So then when we give our variables either let or const down here, different names.
          
          These are the names of the variables.
          
          There are a few different constraints.
          
          First of all, there can't be any spaces.
          
          We can't say something like my space age, okay, It has to be one complete word.
          
          Typically, if we want to use two or more words in a variable, we use something called camelcase,
          
          which is something like this.
          
          My.
          
          And then we start the next word with a capital letter, my age.
          
          And it's called camelcase because it looks like the hump in a camel's back.
          
          All right.
          
          So that's the first constraint.
          
          We can't use spaces now.
          
          The second one variables can contain only letters, numbers, underscores or dollar signs, but they
          
          can't start with a number.
          
          They can start with any of those other things I just said underscore or dollar, for example.
          
          But they can't start with a number, so I can't say five age notice.
          
          We get that squiggle line.
          
          Okay.
          
          And finally, there's also some reserved keywords in JavaScript that we can't use for variables because
          
          they're used for other things, for example, let and const.
          
          So I couldn't create a variable that's called const, right?
          
          That won't work because this is a reserved keyword in JavaScript for creating constants.
          
          So what I'm going to do is leave a document attached to this lecture which is going to outline all of
          
          those different reserved keywords so you can check those out for yourself.
          
          So that's just a few constraints when we're naming these variables.
          
          Also, when you do name things, try to make the name meaningful so that if another developer looks
          
          at your work, they would say, Yeah, I understand what this is meant to represent.
          
          Okay, so one last thing in this video I'd like to show you, and that is comments.
          
          So say, for example, I want to make some kind of comment in this code for another developer.
          
          When they come to read this file.
          
          I could do that.
          
          I could say something like this, log things to console.
          
          And that right here is grayed out this is a comment and it's a one line comment.
          
          And I did that by adding two forward slashes at the start.
          
          So when JavaScript runs this file, it's not going to run any comments.
          
          I can also use these to comment out certain things like this.
          
          If I just want to test something with a console log, but then don't use it later on I can comment it
          
          out and now this is not going to run.
          
          So that's how to do a single line comment using double forward slash.
          
          I can also do a multi-line comment by saying forward slash and then asterisks and then we need to close
          
          this off at the end of the comment like so and we can place it somewhere else.
          
          We could place it up here.
          
          And then all of this is commented out as well.
          
          Okay.
          
          So that's just a couple of ways to comment things out.
          
          Also, there is a keyboard shortcut.
          
          I could highlight something I want to comment out and press control and forward slash and it's going
          
          to comment those out.
          
          I can do the same thing to uncomment those as well.
          
          All right.
          
          Now there is just one final thing to promise in this video.
          
          I want to tell you about, and that is when we're using modern JavaScript techniques like let and const
          
          and other things in the future, they might not work in 100% of browsers out there, old browsers like
          
          Internet Explorer 11 and things like that.
          
          They don't support a lot of the modern features.
          
          So I would strongly suggest that you use something like Google Chrome as your browser for this course.
          
          In one of the last chapters, we are going to look at supporting all browsers using things like Babbel.
          
          But for now I would recommend using something like Google Chrome for the rest of this course.