Welcome back! Up to this point we've played around with some JavaScript types.
Only 3 out of 7. Don't worry, we'll get to the rest of them.
But there's one problem.
How does the program remember things?
For example, if we write "These pretzels are making me thirsty".
And we'll do this.
"+" We need the exclamation "!".
OK.
I enter that.
OK. I get that. That's - that's good.
But if I need to use this again.
Well. Again I have to write "These pretzels
are making me thirsty".
And imagine if have to do that hundreds of times.
That's kind of annoying right?
That's - computers are supposed to help us but instead we just keep writing stuff and keep writing stuff.
And that's not very efficient, is it?
Well to catch and hold values
JavaScript has something called variable's. And variables can be used
with the word 'var'.
So now if I go 'var george', so 'var george = "These pretzels are making me thirsty"'
plus [+] "!".
Notice the 'semicolon' here - I'll explain what that does in a bit.
I press 'Enter'. I get 'undefined'.
Let's not worry about that.
That doesn't matter for now.
And now if I go 'george' and you'll see that Google Chrome actually helps me and recognizes it.
I press 'Enter'.
"These pretzels are making me thirsty!". How awesome is that?
So we're able to store this value.
And obviously this is simple, but imagine, this was a long calculation that we had to make and we want
to access it.
Well, all we need to do now is have 'george'.
It's a 'variable'.
So now you see in the last video when I said that when we're comparing things we have to use '==='
Well because when you do '3=3' it's saying: 'Assign the number 3 to 3'.
But there's a rule with variables. With variables
you can't start with numbers. So if I go 'var 3 = 3', I'll get an error.
But if I go 'var three = 3', well, that's no problem.
And variables have a few rules.
The rules are: Well, it needs to start with a letter.
It can end with a number, that's fine, but it needs to start with the letter and it can't start with a symbol.
So I can do 'var &hello = 5'. Now I'll get en error.
So a variable needs to start with the letter, can end in a number, that's fine.
And there's a few other special cases. A variable can also start with a '$' or '_'.
But we don't need to worry about that too much and the standard and JavaScript so you won't get an
error.
If this happens, but you do something called 'camel case'. So if it was a word, let's say 'firstname', you
do 'camel case', because these are two separate words. You do lowercase
the first word. Second word is going to have uppercase.
So 'firstName'. You've capitalized the first letter and that's called 'camel case'.
So still not getting what variables are? Well, I'm going to demonstrate this point further
and why we need to store values in a programming language.
I'm going to introduce you to a little cool trick called 'prompt'. And 'prompt()', when I use these brackets
and I press 'Enter'...
Look at that.
I've got something here and it's asking for something and I press OK.
And look at that!
It returns whatever I typed.
Let's try that again. If I go 'prompt()',
press 'Enter' and say 'Hello', press 'OK'.
I get that response back.
OK.
So how can we say something like 'What's your username?' and store that value?
That's something that we use quite a lot on websites, right? Well, with 'prompt' you can do 'prompt()'.
Then here we'll say "What is your username".
So we're entering the string here.
And if I press 'Enter'.
OK, so it says "What is your username".
So now if I type in my username is "andrei", I get the 'username'. So using that knowledge,
let's make a calculator. Let's do 'var first = prompt()'
"enter first number"
And then I 'Enter' pressed 'Shift' here, so that I can add a new line. So 'Shift', 'Enter' and I'm going to say
'var second = prompt()'
"enter second number"
Now if I press 'Enter', I'll get 'enter first number'. I'll say '5', press OK, then 'enter second number' and I
say '10' and press OK.
OK.
But now I got 'undefined'.
But here's the cool thing.
Now I have these values held in these variables, so I can use them. If I check 'first',
I have "5"; if I check 'second',
I have "10".
But you'll notice that I have double quotes [""] around them.
And that's because 'prompt' automatically changes it to String, it's expecting a form of text.
So there's a trick to change a String into a Number.
And all we need to do and this is just syntax we need to remember is 'Number(first)'.
And it'll give us '5'.
So all we need to do now is do 'Number(first) + Number(second)'. And we'll get the sum of these 2.
But again we can use a variable here.
I don't want to keep typing this.
So what if I do 'var sum = Number(first) + Number(second);'
Press 'Enter'. Nothing yet, because it's in the 'sum' now.
So if I asked for 'sum, I get '15'.
OK.
So let's write our first program here.
I'm going to show you one other cool trick and that is 'alert'.
So instead of 'prompt' we're going to use 'alert'. And 'alert()'.
What it does is - it says "hi there!"
If I press 'Enter', I get a pop-up, but without anything to enter, just an 'OK' button.
So using this we can create a calculator.
So let's do this.