Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          All right then, gang.

          So we've taken a look at numbers, strings and also arrays.
          
          Now I want to turn our focus to null and undefined.
          
          Now, these two types are quite similar in that they both represent a complete lack of value.
          
          The only difference is that Null is an intentional lack of value, but undefined isn't.
          
          Now that might seem a bit odd when I'm talking about it now, but you'll see exactly what I mean in
          
          a minute, So let's do a few examples.
          
          First of all, I'm going to create a new variable and I'm going to call that age.
          
          Now, I'm not going to set it equal to anything.
          
          I'm just kind of setting this variable up, but not assigning it a value yet.
          
          Now what I'm going to do below is console dot log three things.
          
          First of all, the age variable, then age plus three.
          
          Then I'm going to output a template string to say the age is and then we'll output the age inside that
          
          template string.
          
          Okay, so what I'm going to do is save this and just run it in the browser over here and we can see
          
          for the first value undefined.
          
          Now it's undefined because we've not given it a value yet.
          
          Okay, so that's what happens when we don't give something a value and we try to use it.
          
          The browser automatically assigns a value of undefined.
          
          It's not intentional.
          
          We've not intentionally said let age be equal to undefined.
          
          The browser automatically gives it that value when it doesn't have one.
          
          Now then, when we try to use undefined in a formula like this, we get not a number.
          
          And the third one, when we try to output undefined as a variable inside a template string, we see
          
          this the age is undefined, so we get undefined in the string itself.
          
          Okay, so that's undefined.
          
          The browser is automatically giving the variable this value because it doesn't yet have one.
          
          Now I said that null was an intentional lack of value and by that I mean we explicitly say age is equal
          
          to null, so we're giving it this empty value of null.
          
          Now, okay, we didn't do that with undefined, just null.
          
          So if I save this now and preview in a browser now we can see Null for the first one.
          
          When we try to add three to null, we get three.
          
          So when we output null as a value in some kind of formula, it takes on the value of zero.
          
          And when we try to output it in a string, it just becomes null inside the string.
          
          Okay, so that's null and undefined.
          
          Now I said that null is an intentional value, meaning that we explicitly assign it a value of null.
          
          But what's the point in this?
          
          Why would we ever really want to do this?
          
          Well, an example could be that we have, say, a form on a website which asks for a user's title.
          
          Now, when they select the title, for example, Miss or Mrs. or Mr., we store that value in some kind
          
          of variable.
          
          Now, if at some point the user goes through the rest of the form, but then decides to clear the form,
          
          reset it by pressing a button, it clears all the form fields.
          
          At that point, we no longer want to store the value of what they selected for the title, for example.
          
          Ms. We want to clear it.
          
          So at that point we could explicitly say, Well, okay, now the variable title is equal to null.
          
          Okay, so that's null and that's undefined.
          
          They will be popping up in other lectures as we go forward.