In this video we're going to focus on Javascript variables and their syntax.
So variables are a concept that exists in every programming language out there.
And the basic concept is always the same.
A variable is a container that has a name on it and inside of that container we store some bit of data
.
So we saw the basic primitive types.
Things like strings and numbers and booleans and variables are just a place a container a jar where
we can put one of those in and give it a name that we refer to later.
And then of course the name variable means that the data that we store in that jar in the container
can change you can vary.
So variables are really important.
They let us store some data and recall it later.
So we could have a variable called Current logged in user if we had a website with user log in and it
would just know the user name of the person who was logged in.
And it could vary and it would change.
And then as soon as someone logs out it's then set to be empty or false or something we could have another
variable for our tic tac toe game called Current turn and it would switch between player 1 and player
to player 1 and Player 2 and that variable is in charge of knowing or keeping track of which player
is supposed to be going at any given point.
So the syntax for variables in javascript is very simple.
It looks like this.
The VAR keyword V-A are space a variable name.
This is up to us whatever we want the name to be called the label for that container equal sign and
then a value and then a semi-colon.
We will talk a lot more about when you need semi colons.
For now everything we do will be able to get away with a semi-colon or not having some a colon.
When we talk about style and convention we'll talk about that.
But for now put a semi-colon at the end.
So here's three examples of variables.
The first one far name.
So I'm calling this container name and I'm setting it equal to the string Rusty the next one.
I'm creating a variable called secret number and I'm setting it equal to the number 73 and the last
one and making a variable is adorable and setting it equal to be true the boolean value.
So let me show you how this works.
I'm going to go ahead and open up my Consul here
and I'm going to go ahead and make a first variable.
So my variable will be called name and I'm going to set an equal to my wonderful dog Rusty's name and
I'll hit enter and what I've done now is say javascript I need you to make some space and I need you
to give that space a name called name.
It's a little confusing instead of equal to resti.
So now whenever I ask for a name I just type the word name.
It's going to give me Rustie or whatever is stored in name.
So the next thing is that I can change the value that's in there.
So to do that I just reassign it.
I just say name equal sign and I'll change it to be Tator that is terrible nickname I give him.
And now if I ask for name again it's now Tator.
So that's the important part of variables.
They can change.
They are variable.
So in this example I'm recalling the value of name and then concatenating it with Hello there.
So I'll just show you that.
Let's make another variable this time called friend with our friends equals Sally.
And then all I do is say friend let's do a hello there.
Hello there.
Plus friend and I made sure to add a space here so that when we add them together we have a nice space
in our sentence and we get hello there Sally.
And then if I change that and I now say friend is equal to no one.
It's very sad.
And I write the same line Hello there plus friend I now get hello there no one.
So also just show to you we can store numbers in there as well.
Can or any of the basic data types.
So I'll make a number called Far numb equals.
Let's go with forty three.
And now whenever I refer to Nom I get 43 and I can do math with that so I can do something like no.
Plus one hundred and I get 143 or I can do numb my mide two and I get one.
Another important concept about variables and Javascript is that I can change their value but I can
also change their type.
So let's say that I want to change the value of knowm to the string 50.
I can do that very easily like this numb equals.
And I could either write that number 50 in there or the string the word version of 50 and it's OK that
it's a string.
It used to be a number before hand but I now change it to a string.
Javascript doesn't care as a side note in some languages.
That is a problem and you can't do that.
Javascript has something called dynamic typing which means that it can change from one type to another
without problem.
So the last thing about variables that I want to touch on is that there's a convention for how we name
them.
And if you notice here they all follow camel case and that means that the first letter is always lowercase
.
And then if we have another word we uppercase the first letter of the new word.
So camel case looks like this camel case and snake case which is another case you may hear uses underscores
between words and there's one more often called Khabab case or Dasch case which is you use a dash between
words.
So javascript variables should be camel cased