Learn JavaScript #3 | Integers, Strings, and Variables

JosephDelgadillo
58 followers
13 Video Views·Aug 16, 2024
Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/
We are going to continue with this complete JavaScript tutorial by covering basic data types. In this video I will show you how to use strings, integers, and variables in JavaScript.
Learn more about my courses at https://josephdelgadillo.com
So, a couple videos ago I had said that if we don't use
quotations here it's going to think we're passing in multiple values each
one as a variable. So when you write a word in JavaScript that is not in quotes,
JavaScript doesn't know that it's supposed to just be a string or a sentence,
for instance. So we tell it that it is by using quotations. So, we're going to be
learning a bit more about variables and strings in this next line. You create a
variable in JavaScript by using the keyword var, and notice how it turns blue
in my editor, and I'm going to hit space and now we're going to name the variable.
So, I'm going to name it welcome_message, and then you put a space and an equal
sign, and you're going to assign a value to be used every time we use welcome
message in our code. And so in this case it's going to say "This is a welcome
message stored inside a variable." We're going to end that with a semicolon. Now,
this is a string. It's going to be used as though it were text. This is not. This is
going to be used as though it has a value inside of this, but this is not the
value. This is called a variable. So what I'm going to do down here is I'm going to
alert welcome_message, and what's going to happen here is when the
page loads it's going to call the alert function and what it's going to pass into
it to you know use as the content of the alert is going to be welcome message, but
it's not just going to print out welcome message, is that a capital L? No, it's just
how it slanted it there. It made it italicized, rather. It's not going to
print out welcome message it's going to print out the value that we've assigned
to that welcome message. So, it's you know you can rather than printing out
if we wanted to to do this multiple times, we wouldn't want to write this out
multiple times. We're just going to you know
alert that multiple times. Let's save it and refresh, and as you'll see it does print
out the value of the variable not the variable name. So, that's what a variable
is and that's what a string is. Now, these are types. So what a type is basically what
type of type is this...what type is this? So, this type is a keyword, this type is a
variable, and this type is a string. Let's say we want to create a variable called
age, or rather myAge. We're going to set it equal to my age which right now
is 28 I believe...that sounds right. Alright, so, now notice that I did not put
this into quotations, that's very important. Integers, or full numbers, do
not go inside of quotations unless you want them to be a string which you can't
do like math on for instance. So this is a number, it's a number. Now, a float is
kind of like a number as well. So, let's go ahead and say
exactAge. I'm going to set that's 28.23, sounds about right.
So, what this is, is a float. And what that means is it's a floating-point number, it
has a decimal in it. So if you were writing out $28 and 23 cents, for
instance, it would be 28.23 not just 28. So that's how you would write it
in JavaScript. This is a float, and this up here is an integer. And we're going to
be discussing those more at some point, but for the most part these you can play
around with these now. You can actually do math with these because they're numbers,
but if we put them in quotations we wouldn't be able to do math on them.
And, I'm going to show you guys what I mean right now actually. So I'm going to
remove this and I'm going to print myAge plus exactAge. I'm going to save it,
and now when I go back to refresh this this is going to print out
56.23 because it knows that it can do that operation on this. It can add
28 to 28.23 because they are numbers. What happens when I
change both of them to strings like this by putting quotations around them and
making them text instead of numbers? I'm going to refresh and it says it's
2828.23, and that's because it's just putting
those two strings together as though they were this, alright. So, that's the
difference between a string and an integer, well that's the main difference
I guess. Another thing is that we can't add an integer to a string. You may have
guessed this, and we can't add a float to a string. So I'm going to say
welcome_message plus exactAge. So this should print out this is a welcome
message stored inside a variable 28.23. That's what we
would expect to happen. So, okay refresh. Oh, it did do that, alright. That was
an integer. Okay, was it the console.log that didn't work in.
So, it converts it all to a string now. That never used to be that way, at
least as I'm thinking. We can also separate them by a comma here, and that
leaves this as a floating point, so if you need to use that later on you can.
And then we also have other types. You can use single quotes for strings as
well, but we're just going to leave it at this for right now. So, you guys now know
how to create variables, strings, integers, floats, and you know how to call a few
functions. So, moving on we're going to be discussing more about basic types and
basic things you can do with them.
