Welcome back in today's lecture, are we going to talk about JavaScript variables we are going to see
about variables, alert function, prompt function and undefined data types or JavaScript types?
Remember, that was from the first lecture.
And I've told you that we need to do this later in the course.
OK, so open your vehicle, start new lecture, call it lecture, dash three JS.
Make sure that you link it here first.
Copy this one.
OK, then we need to comment it out and make sure that you changed it to three.
OK, now our browser should be like this.
Actually, we don't need that one.
No.
Variables are something that is very crucial for JavaScript.
So let us to store values inside this variable.
So consider them as a container so we can add some data, we can store something inside.
So this way, we are giving the program a memory where we can store data.
So variables hold.
Any JavaScript data type so they can hold any data that we have just covered.
OK, so let's call it here variables.
JavaScript variables helps with having variables, and so first things first.
We need to first specify that we are talking about the variables and how are we going to do this, we
we are.
And then there is goes the name of the variable and there we can specify the data.
And now this.
Another operator comes to place equals means we are assigning data here.
All right.
So how we can print out the variables to consider that log name.
Let's call here my name and take notes, something that I did here and I will just explain a little
bit later.
So go back to your browser and here it is.
Let's change it to more meaningful, Rick, or you can put Andy doesn't matter your name, so why variables
are so useful.
OK, let's do it like this.
And I'm going to show you, First Consul, that Locke.
Five plus nine and save it, so that will give us a result 14, but if I want to access the same result,
I need to copy the same line of code and execute it here.
Right.
So that will give me to fourteens.
Well, this is not ideal.
This is called code repetition.
And there is a rule here in not just in JavaScript.
Or any other programming languages, it's called dry.
Do not repeat yourself, so it is a good idea to find a solution how we will not be able to repeat ourselves.
So that's where variables comes into place.
Now, what we can do here, I'm just going to recommend this one, for example, variable.
Let's put it here somewhere and I will put five plus nine.
OK, now if I go back here, there is nothing there, right?
But I could call it log just some because the very variable, some holes in the memory, this data 14.
Right.
And if I create another variable called result, then I can still use this some some minus five for
example.
And that should give me.
Nine.
And I need to also consulate like the result.
So now you see the power of variables, we can we use them because they're dear, they're defined,
they're they have a value.
And now we can use them in the course of if I want, I can just again use the result and I say.
Let's make it here.
This is the result from the variable.
Semicolon there, plus make a breathing space, make sure that you close the semicolon and there you
go.
Now I have nine again.
I have nine here so I can reuse it.
That's called reusability.
So that is pretty much it.
And now let's talk about the part here, the notation, the way we are just giving the variable names.
Now, it is very important for us as a developers to create good variable names.
A variable should accurately identify a variable name, actually should accurately and identify your
variable.
Right.
For example, if I say my name, then you should as a developer from outside to expect that, well,
this will hold a string with my name.
Right.
But what about this notation?
It's going to call it's going to be called cavalcades notation in how this notation works.
Now, when we have a variable that is built from two words like first.
Name, then we are going to use the Campbell case notation and how this Camil case notation works.
Now, the Kemakeza notation works.
The first word is going to stay with a small first letter.
So it's not going to be capital letter.
But the second word will have a capital first letter and it will be this first name.
So throughout the course, I will use Camil case notation and you should also do the same thing, but
in future.
If you see somewhere or in some drills that they use some other notation, then that is OK, it's going
to work.
Now, another rule, how we can name our variables is that the very variable names cannot contain spaces.
For example, variable, my name, it's not going to work.
It's contained spaces.
Another thing that the variable you should know that they must start with a letter on their score so
they can start with the underscore and you can continue with combinations of letters and numbers.
Right.
So you can even have a numbers.
You can start with underscore.
We can start with the letter or you can start with a dollar sign.
So let's do it like this, where names can start with letter underscore or.
Dollar sign, right, that is pretty much it.
Now, another thing is you should know that variables are case sensitive.
What that means, variable A B equals.
Eight.
And variable A B equals eight are very different, why they're case sensitive, this is a capital B.
This is a lower B, so they're D variable is completely different than this.
Now, in the memory, we have two different variables, but they also have same value.
But that's different.
And yeah, so variable can contain letters, numbers underscore or dollar signs.
They're case sensitive and they can start with this three.
So you cannot start variable with one or you cannot start variable with a special symbol like this.
And that or even the special symbol will not be able to be applied here.
So if you save it like this, save it, you will have wait.
You'll have unexpected token.
And that was the token that we just put.
Nice.
So let's continue working with our examples.
So what I want to show you that for the first time we will be able to use a function.
So these functions are inbuilt.
So we have access here.
For example, the first is prompt and if I do it like this, F stands for function.
So it tells me that is a function.
But every time I use function, I need to explicitly write these brackets.
So cancel.
Actually I don't need to cancel the log now in semicolon again.
Now if I go back to a browser then the browser will give me a pop up where I can put a data or string
here.
And if he did, OK, now there will be stored somewhere.
Prompt.
So, again, I will not be able to access the data from the program.
Why is that?
Well, because I have in store it in variable, but what I can do is variable data.
That then I haven't told you this, you can write here, what is your name and make sure that this I's
capital looks better.
Now look what will happen here is what is your name?
But the browser is still there.
So, Rick, now, if I hit OK or return, then again, I don't have access.
No, I'm just joking.
So the data variable is here and yeah, I just have access to whatever.
We just enter through that prompt.
So why I just access that data here and then right here.
Console the log in that data.
Well if I do this in I will save it.
Then the entire life server will kick in every other line.
It will go line by line, the compiler and it will delete whatever had previously.
Hold it here, store it here in this data.
That's why sometimes it's very useful, this browser console, so I can just use it.
That's pretty much it.
Now let's do something interesting.
Another function that I want to show you is called alert, but let's spice this one up.
So I'm just going to prompt the user and I will say, how old are you?
Are you and I'm just going to copy this page that now I'm just going to ask about the year and what
I'm going to say.
What is the current?
You're nice and I'm going to use a learned function for the first time.
Now, this alert, I don't have to save it in a very old I can, but I don't have to.
And I will say I was born.
And and semicolon here, space it plus now I don't want to confuse this function, what I will do,
I will get this in a brackets and I will just minus a year with the H.
So why just go with the year will here?
It will be bigger than the H obviously.
And that will never produce a negative result.
So this is a pure mess.
So save it.
Go back to your browser.
How old are you.
Thirty three.
I don't know if you can see this.
How.
Let me just zoom in if I can.
I can actually.
So thirty three here.
OK then.
Twenty, twenty two here.
OK then.
I was born in this year.
Nice.
That is pretty much what the alert function can do in there, out of the box there for us so we can
use it.
So this is to alert the user that something is happening.
For example, you have you have failed to log in or your password and confirm password does not match.
There are some scenarios that we can use all of this and they're pretty much cool.
When I started JavaScript.
I was I was like using this problem in my in my lectures and in my exercises.
And I was pretty much, you know, excited because, oops, I am I'm doing something with JavaScript,
so that's pretty much it.
And the last thing that I just want to talk is the undefined.
JavaScript is type.
Now, this is from the first lecture.
Right, we just get Boolean true, but we didn't finish with the rest of them.
Why?
Well, because, for example, if I do hear variable.
Last name in semicolon and save it and try to conceal a log, last name, so I'm accessing the last
name, but I didn't initialize the value, so maybe I haven't told you what variable my name is or variable.
Any name here is saying, OK, we are going to declare variable with this name and we're going to assign
value after that.
So let me see.
Let me show you what will happen.
Oh, now it's a good idea to go back here.
Just to comment out this part, because it will ask you the problems all over again.
You don't want to see it, so it's going to say undefined.
So why we are having undefined and this is one of the data JavaScript types that we need to discuss
now.
Undefined is meaning that we don't have any value in that variable.
So undefined is that when we try to access something that doesn't hold data.
So, yes, there is a memory triste of our variable called a last name, but there is no data.
So it's only memory reference doesn't hold any data.
And that's why when we try to access it here, it gives us this undefined.
Now, when I haven't shown you is that you can also later on before you use this last name, you can
say you can actually initialize you can give a data and now it will work.
It will say Sokolsky.
Why?
Because some counseling log after I declare and initialize with the data this variable that is simple
as it can be.
Now, that is pretty much what I want to show you.
And as a lasting, um, it's good idea to mention the semicolon.
So one thing that we haven't covered is the semicolon that I put at the end.
So semicolon marks the end of the expression.
Anything that is written as a value like this is considered as expression.
Now, it will be working without Collo nowadays, but semicolon.
So it will be working even without semicolon.
But it should also.
Very good point for you to just start using the semicolon because that will end the expression.
And yeah, it's a very useful MI, as is a teacher, to teach you that you should use it and that will
stay with you.
And you can later on in your programming you will use semicolons.
So that is pretty much it.
Now semicolon is anything that is written as a value, is considered as expression and semicolons puts
always at the end of that expression.
And that is pretty much for this lecture.
We haven't done much and you can come at this one now, but it is what it is.
Make sure that you do the exercises, so I will see you in the next one.