Learn JavaScript Episode #4: Modifying and Undefined Variables

JosephDelgadillo
58 followers
8 Video Views·Aug 16, 2024
Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/
In the 4th episode of this JavaScript tutorial series for beginners we will cover modifying and undefined variables. It is important to reuse existing variables and other objects whenever possible to avoid excessive RAM usage.
Learn more about my courses at https://josephdelgadillo.com
Alright guys, I'm going to remove some of this because we're going to be
discussing more about variables in this video. What I'm going to do is I'm going
to use one variable called myAge, and the first thing I'm going to do is print
it out. So, "My age is" and then I'm going to put a space and a plus and I'm going
to print myAge, and then I'm going to drop down here and you can reassign
values to variables. Now to do this you don't just type out the entire thing
again because what it's going to try and do is create a new variable and there's
already one created and we don't want to do that, and that's a great way to to
cause I guess excessive RAM usage by creating more variables and objects that
you need. What we're going to do is we're going to reassign it just by saying
myAge just the variable name equals 29. And then I'm going to console.log "My age
next year will be" this. Now I'm going to save it, and so what we're doing is we're
creating a string and we're adding an integer to it which makes the integer
become a string as well, and this is called concatenation. You can also do
this with just two strings. If I wanted to write you know hard-coded in there
this would print out as my age next year will be 29. But for the purpose of this
video, we're learning about reassigning values, so I'm going to set that to my
age, I'm going to save it and refresh, and it says my age is 28. Then the variable
gets reassigned and it says my age next year will be 29. Now another way to do
this would just be adding one to my age, so this is a bit of math. What we can do
is myAge + 1 and save it. Now what happens here is the same thing, this is
my age next year will be 29, because it takes my age and it adds 1 to it.
Now, if we only want to add one to it we can use an extremely shorthand version
and say myAge++. Save it, refresh, and it didn't change it and
that's because I stored it in there itself. Save it, there we go. So, basically
what this is you don't even need to write the equal sign or anything, this
just increases the number by one. This is actually a feature that was first
introduced into C++, and that's why it's called C++ actually, because this was a
an awesome feature to be added to it. Well, that's not why it was called C++
but it's fitting that C++ was as far as I know it was the first language to do this.
So, that's how to increase a number just by one. You can also do the same thing in
reverse by negative negative. So, let's change that to my age last year would be
27. And so if you're using integers and you want a quick way of you know
modifying value by one you're going to use those, otherwise you can set my age
plus equals 2. I'm going to save it and refresh, and what plus equals does this
right here is essentially the same as writing my age equals my age plus two. So,
you can see what's happening, I hope. Rather than writing the entire thing out
like this, my age plus equals knows that we're going to be adding to this
directly and storing it in it's own variable when we're all done adding to
it. So my age now would become 2030. So that's a bit more about variables. We
need to discuss, I mean you can do the same thing with with text variables. So,
create a variable called myName. I'm going to just do that you can actually
declare variables without even assigning a value to it. So, this is just an empty
variable and if we try to use it, I'm just going to change this one right here
"My name is" myName, save it and refresh...I forgot the plus sign there.
Save it and refresh, and this says my name is defined, and that's literally because
we have not defined it yet. By declaring it, or assigning a value, you're defining
the variable. You're telling the variable what it is. Without that it is undefined. Which
we'll be using some if conditions, we're going to be able to test if something is
or is not, and we're going to be learning about that soon but not right now. So,
let's go ahead and in the next video let's cover another type which is true
and false.
