In today's tutorial we are going to learn about variables used in JavaScript.
So stay tuned.
[music..]
Hello and welcome to another new episode of learning simplified.
In today's tutorial we are going to learn about variables.
So what is this variable?
In defining that the first thing that we are going to do, first of all
commenting it out and let's say we are..
Now if we reload we can see that this is the output.
In this particular case suppose that this particular string it has to be printed
5 times in this whole page.
So if we call this thing into our body part, all we have
to do we have to write this or initiate the JavaScript here; so that is what we are doing
first and then all we are doing we are calling it 5 times into this whole page.
This is not necessary that it should be 5 times; it actually could be dozen off times,
it could be hundred of times, and it could be into some different page; it is just an
example of a possible occurrence so take it as an example.
Now if we press Control + s and get back to your project and reload we can see that this
thing is now printed 5 times.
Not actually 5 times it is being executed six times cause
once you have called it over here.
Now suppose I want to change this string; instead of
hello world I want to type this particular stream hello cruel world.
Now if I want to make this change to be reflected along the
whole page then all I need to do I need to write on the same thing here..
Now place Control + s, get back to your project and
reload.
And now you can see that it is now getting changed here; instead of hello world
it is now executing hello cruel world.
But this is not the exact same thing that we were
intending for was there maybe dozens of other instructions or comments or code; this is
an HTML page so you are using other HTML contents over here, you could use other
programming languages time to time, so your page will be getting bigger and bigger and
it could be an error that you forgot to change this particular phrase this particular
part and you forgot to make changes over there and when you Press control + s and you
could see at the beginning, hello world is being shown here.
And rest of the contents those are getting hello cruel world display
in printed format.
So this is practically the exact necessity due to which the concept of variable is taken
into account.
Instead of repeating our syntax,our instruction, if we just write here -
'var'; 'var' means variable, that means you are declaring that a variable is to be
introduced in this particular place.
Now you are using var and you are assigning a name.
Hot say we are saying 'var phrase' or anything else that you want that means a variable
name has been declared over here.
Now if we always put the semi-colon at the end; now if
we copy this thing from here and paste it over here - a variable has been declared whose
name is 'phrase' and inside this 'phrase' we are now assigning this equal sign here
which practical denotes the assignment operator.
It is practically an assignment operator but you may call it an equal sign if you wish
to; so this is an assignment operator and it
assigns this particular string "hello cruel world how are you"?
Against this particular variable name phrase.
Now all you can do you can call in this name.
Just copy it and do not put it inside a quotation mark just call
in the variable name over here.
Now just press Control + s and get back to your project
and reload and now you can see that the only change you have made in one single place
it is practically here and nowhere else.
Now for say, instead of this particular string we are intending to use a particular
numeral against it and we are just removing this variable from here and we are assigning
a number and now if we call this phrase everywhere we can see that this 112 number is
getting printed.
That is why we use variable ,we are practically by using this variables
we are assigning some text from string some numbers or anything else against this
variable name and we are calling it anywhere on the same page or in the different page
that does hardly matter.
You have assigned it only in a single place, you have named it
and then you have called it.
It is like the same as you have done in case of assigning a
particular selector name into a class name and then calling it into your main HTML page.
So this is the thing that a variable does.
It practically saves you a lot of time and you
do not have to repeat your things again and again and this is the main cause that you
are practically using a variable.
Now there are some limitations about it.
Here all we are doing we are just using this variable name.
Press control + s and get back to your project and reload and you can see
that this output has been executed over here.
Practically all this variable names those are very much case sensitive.
That means you have used here variable N-U-M num and it is
written in all small characters but if you use this kind of spelling over here; the
spelling will be same but the character casing will be different and that is what we are
seeing over here.
So if I use this kind of thing over here....
Save your project get back to your page and reload you can see that
there is no output.
Variable is very very much case sensitive.
This is the first one, the second thing is, you may start your variable name with this
one, and you may call it here; that means you
are assigning a variable name as _num and you are calling it by name here at this
document.write(); Now place control + s and get back to your project and reload and you
can see that this thing is now working but in case if you use this kind of property,
for say I am using this one this Dollar sign, this Asterix sign, this one - all these
special characters and at the end I am using an @, and what I did over here I just
copied it and and I am placing it over here.
Now if we get back to our project we can see that this variable naming is practically
not working.
That means you cannot use any kind of special characters at the beginning
of the variable names and at the closing of the variable names.
You need to assign your variable name with letters, or with numbers,
or anything, but do not assign any such things such as this special characters that I
have used over here.
This is the second limitation of javaScript.
The third one is, you can put numbers at the end of your variable name and you can use
that same variable name here.
Get back to your project and reload and you can see that
it is back in action.
What what if I put this number this 112 number at the beginning of
the variable name and calling the same variable name here.
Press control + s, get back to your project and reload and you can see
that there is no display.
That means you cannot start your variable names with a number.
If you wish to have a particular letter, for say, I have assigned this variable name
as a and I have called this same variable name here; pressing Control + s and get back
to your project and reload and we can see that it is Back in Action.
But instead of a, if we put here 1, and we call it here.
Now on reloading we can see that there is absolutely
no display over there.
So this is the third limitation that JavaScript will have.
So you cannot change the casing characteristics of JavaScript is JavaScript
is a very case sensitive programming language.
The second thing is you cannot use any kind of special characters over here
and the third one is you cannot assign any kind of number at the beginning of the
declaration of your variable names.
So this is the part that we need to know from this tutorial.
In the next tutorial we are going to learn a lot more about variables,
such as concatenating this variables.
Hope you guys have like this video if you guys
have like this video then do not hesitate to
hit that red subscribe button down below.
Share us, like us, comment on us..
Hope to see you guys in our next tutorial.
Till then, bye.
Không có nhận xét nào:
Đăng nhận xét