how is it going everyone
and welcome back to our next episode
in how to create variables using java script
variable is something that we use often inside java script
we actually use it all the time
and in this example here we are gonna show example how variables work
so you can see how use it inside java script
as you see from here
we have index page open in the right side
and in the left side i have a browser open in the command console
or the developer tool open in the bottom here
now what i'm going to do is going ahead and right
some java script code i'm gonna say i want to
console dot log
which is going to send some kind of information
to the command console inside the browser right no
what to say ten
plus twenty
you guys can see we get thirty inside console
that takes exact same thing if we go inside the console
and write ten plus twenty
we get the same result
this is how can actually we write inside the console from inside out
document inside the index page
now a variable is a type of a container that we've use inside the
java script in order to store data inside of
so right now what if we go inside my code here
i could actually go ahead and create a variable
or declare a variable which we called
by using a keyword called var
now the var keyword says that we now declare the
variable then we need to name it something
when it comes to naming variable as you can name it any kind of name you want
this just a couple of things you need to keep in in mind
when you name this variables first of all you can start with a
number and second of all capitalized letters
and non capitalized letters
do actually matter
so want to create variable called A
semicolon and then create a variable called
capitalized A these two are separate things
and separate variables and they are not the same thing
now to create a third variable by declaring
a new variable and calling it something that has
multiple words inside of it then it is a habit
of programmers to start with a non capitalized letter
and then the next word is going to be capitalized
so i want to say a variable is called denial
nelson which is my name then i want to start
with non capitalized d and then a capitalized
nelson we can write two words
inside a variable name so we can do this
in order to create a variable name cause this is not going
to work inside the browser now when it comes to variables
we have different types of variables we have something called a var
variable and we also have something called let
and a const variable
now because we still in the beginning of the course i'm not gonna discusse
let and constant to later in this course
because we use this variables for different reasons that
we haven't talked about yet as of not to confuse you
i don't want to talk about let and const until later in this
course so right now you guys can see we did actually declare a variable
called a and what we can do we can actually assign
it something so we actually put some kind of data
inside variable and the way we that
is by going to next line and because we already created
the variable or declaredit
we don't need to do that again so we want to refer this variable a
i just need to write the name of the
variable which is a and set it equal to
some kind data i'm gonna say equal to ten
we can also said equal to a piece of
text or a function or something that we have
inside java script but right now we just gonna go and use numbers
now we have a variable called a i can
go and create a variable called b now the first example
we did actually create the variable first and then we gave it a value
the other way we can do it is we can also go and create
variable first by calling it b and
then directly giving it some kind of data
so we can actually say b is equal
to twenty this actually would be shorter
but in some cases this might not be what we want
to do depending on that application we create using java script
but for now in this example over here we can go ahead
and just create a variable called b and set it equal to twenty
we can actually do the same thing here but just simply
setting a equal to ten now
what i want to do is want take these two variables
and add them together and insert it inside the console
inside the browser and the way we can do that
is by creating a third variable
i'm gonna call this one sum or something
set it equal to a plus
b semicolon
and then i'm gonna go and grab the variable sum
and insert it inside the console log here
then i'm gonna save it and actually you can see we get thirty
inside the browser
and we probably change the number and see the difference
the last piece of data inside the console
so now we can see we get forty when i changed a
to twenty this is how we create variable using
java script and this actually a couple different ways we can do same thing
so want you to go down and next line here
we go ahead and declare a variable called a
comma b
comma sum
semicolon and then down below we can actually add the
values to these variables so i 'm gonna say
a equals to twenty
b is equals to twenty
and sum is equal to a
plus b
and this would be the same things we doing up here
the main difference between these two is that i just went to head and declared
all the different variable names inside one line and
afterwards stored value inside inside the variable names
now a third way we can do this is by going down next line
and say we have variably called a
that is equal to twenty comma
b that is equal to twenty
comma and then we have sum that is
equal to a plus b
semicolon all of these does the exact
same thing it is just a different way that we can write all the different
variable types now just want to point out that why i'm going to make
variables at the beginning of this course here is going to be used
in the first method which is the one up here since it is a bit
easier to over look to a new java script now
another thing i want to show you is we just don't use numbers
inside variables when can also use text or functions
or other kind of data we have inside java script so
if i want to take variables a
which is equal to twenty and changed to
what we call a string which is a string of text
i'm gonna say double quotes and then put some kind of
text in between of the doubles quotes which is how we
create a text string in java script
so i'm going to say my name is
denial save it and then you can see
we get my name is Daniel inside the console
it is very important to point out that you don't write a text string
by not including a double quotes
as if you see i save it would you actually get
uncaught syntax error inside the browser because
it doesn't know what kind of data we trying
to put inside the console now I if i
want to take you all the way back to the beginning in this lesson here
if i have a variable called a that doesn't have any kind of
data assigned to it afterwards actually i'll put it
inside the console
you can see that we get undefined and that's because right
now even though we have declared variable called
a we didn't assign any kind of value to a
yet which is we call undefined which means that
right now there is no data inside variable a
and the reason i want to point this out because if
go ahead and say we have not variable a
but instead we have a is
equal to ten
and then save it you can see we get ten
inside the console inside the browser but wait a minute
we didn't declare a variable a
which is very important to do each time you create a variable using
java script so what is going on here
now if i want to write variable without actually declaring it
first then running it inside the browser then
it is going to declare a variable automatically when we run the script
and this is a bad thing because it allow for
error inside our script and later on
we are gonna talk about variables call lit and const and how we fix this
issue where the code automatically declares
variables inside java script
so this is what i want to talk about when it comes to variables inside java script
and just remember we use variables all the time when we create
scripts using java script so this is very important to remember how
to create variable and how we using them inside our code
so in the future we will get more into variables
how we use them to create scripts using java script
so i hope you guys enjoyed and see you next time