Welcome back.
So it's finally time that we actually start learning about javascript and its syntax in the first place
so we're going to start with it's basic builtin primitive data types.
So we have a few objectives here.
The first is that we want to understand the five primitive data types and Javascript.
Next we want to work with numbers and some of the built in numeric operators.
And then finally we're going to talk about strings and common string methods and operators.
So one of the big ideas at the core of every programming language is the languages ability to differentiate
between different categories of data.
So for instance a language could differentiate between a number and a word or it could differentiate
between a whole number and a fractional number or a positive number and a negative number.
And so these vary from language to language.
So in javascript's there are five primitive data types.
There are five low level basic types of data.
And we're going to do a quick overview of the five first and then we're going to dive into detail in
the following slides.
So the first one is numbers and I have three examples here a whole number for a fractional or decimal
number 9.3 and a negative number negative 10.
And I chose these three because I wanted to show you that javascript doesn't care if a number is whole
or if it's fractional if it's negative they're treated all as just numbers.
It's a broad category.
So other languages do differentiate but javascript doesn't.
So again numbers it's just a category for whole numbers fractional numbers and negative numbers.
The next category that we have next data type are called strings and strings are basically text.
So the important thing is that they're inside of quotes.
You can see here we have two examples.
So we have the word hello space world inside of quotes.
That is one string even though it's multiple words.
It's one string.
Same thing here.
This is a number 43 but it's inside of quotes.
So it's actually considered a string to Javascript.
So we can have numbers we can have any character inside of a string.
So the next type is the boolean and booleans only have two options.
They are either true or false.
There's no quotes there's no numbers.
It's just the word true or the word false and that's it.
So why we would actually use these will become much clearer in a few videos from now.
But I just want to let you know booleans exist.
True or false yes or no.
And then there are two more types.
No and undefined and these are actually values.
So they're not really a category.
There's not multiple types of Noall or multiple types of undefined like there are for numbers of strings
.
There is only one null and there's one undefined that just values.
And we'll learn a whole lot more about these in the next video.
But I just want you to be aware they exist.
So numbers strings booleans null and undefined.
So let's dive a little bit deeper into javascript numbers and to do that I can actually open up my javascript
console because I'm in the browser right now.
This is a web page that I'm on.
So I'm going to open up my console command option j feel free to do this.
You can do this on any site and I'm going to start typing some code and the first bit of code that I'm
going to type is really really simple.
It's not going to do much.
Just going to type a number and hit enter.
So in the console when I type a value like a number and I hit enter all that happens is that it spit
back at me.
It's just returned to me the value just shows back up so I can type a negative number negative ninety
nine hit enter and I get negative Friday night.
So I know this is very very basic stuff.
We're not making any exciting applications just yet.
But bear with me very soon will be using these numbers to do more interesting things.
So the first thing that we can do with these numbers is simple mathematical operations.
So these are things that you're already familiar with from basic math a way from the computer.
So things like addition multiplication subtraction and division and the way that those work we just
write some simpler expressions so a numbered like four plus sign 100 and you may notice I use two spaces
here on either side of the plus sign.
That's not mandatory.
So if I hit Enter right now I get four.
I get 104 but I could also do something like three plus seven with no spaces.
And that works as well.
So those are simple operators.
I can also chain them together so I can do five plus four plus three and they get 12.
And I can use other operators too like division.
So let's do six divided by two.
I hit enter and they get three.
I can also do things like one divided by three and you'll see I get zero point three three three repeating
.
So just to wrap up here we also have subtraction.
Let's do one minus 54.
I get negative 53 and then we also have multiplication.
So two times five is going to give us 10.
Another important concept is that javascript follows the order of operations that all regular math follows
as well.
So something like three minus eight times 24 is going to follow the order of operations where the parentheses
will denote that three minus eight needs to be done first and then multiply by 24.
So we get negative 120 at the bottom of the slide.
There's another operator called Maggiolo and Marcelo is usually unfamiliar to my students who have not
done any programming before.
It's often called the remainder operator and in some languages it's also called Moggi lists what it
does.
First of all it uses a percentage sign.
So division is a slash.
Multiplication is the star asterisk modulo is a percentage sign.
So we can type a simple one like 10 mod 3.
People often say modest a short cut and what it will do it will take three and divided into ten as many
times as it goes in as a whole number.
So that would be three times and then it will take the remainder.
So the remainder three goes into ten three times which is nine and the remainder is 1.
So if we get one.
So as another example lets take 20 maade five.
And in this case 5 goes into twenty four times.
There is no remainder.
So we get zero.
So the next day to type that we're going to focus on is the string.
So strings are text.
They are words numbers characters inside of quotes and those quotes can be single or double quotes.
So just as an example we were down to the con..
And we can type something like a low class hit enter and just like the numbers before it just spits
the value back at us so we can do the same thing with single quotes.
Dogs are awesome single quotes hit enter and it spits it back out to us.
Notice that it does show it to us in double quotes.
That's because it treats them the same way.
One small note if we did something with double quotes like hello.
And we ended it with a single quote that gives us a problem.
They need to match.
One other note about single versus double quotes is that I can have a string that uses both.
So I can have a double quoted string with a single quote inside of it.
For example if I wanted to have a string that said I can't stop eating candy I have a single quote right
here in between the end and the T.
That is totally valid because I'm using double quotes on the outside.
If I was using single quotes I can't stop.
Can I use single quotes.
We have a problem.
You can see that it thinks the string ends right here.
When in reality I wanted it to end over here.
So one of the things that we can do with strings is add them together just like with numbers.
Or we could write one plus five.
We can do the same thing.
We can have a string plus another string.
And what happens is javascript will combine them into one string and that's called concatenation.
So if I do this high plus goodbye and I hit enter I get one string.
Hi goodbye.
So if I wanted a space in there I would just need to either add a space here or add a space here and
then I get high goodbye with a space in between.
So that's called concatenation.
So sometimes we might want to actually have a double quote inside of a double quoted string.
So maybe we have something like she said good bye like this.
And I want this to be double quoted.
I don't want to change it to single quotes.
There is a way I can do that and it's using something called an escape character.
So javascript escape characters all start with a backslash and what they are they are ways of escaping
out of the string and writing special characters that might not be valid in the string.
So backslash and then followed by a double quote is how we tell javascript that we want a double quote
in the string.
So if I hit enter you'll see that it's actually just turned into this.
So let me show you again without the backslashes.
And if I hit Enter we get an error.
That's because it thinks this is the first string.
This is a second.
This is in no man's land.
But if I go back by the way I'm hitting the up arrow there.
So if I hit the up arrow it will bring back previous lines of code so I don't have to write all of them
again.
So if I bring this back with the backslashes I get she said backslash quote goodbye backslash quote
.
It just gives me she said goodbye with no backslashes.
So there's another escape character.
If I want a backslash in my string if I actually want that character to be there I could do something
like this
to see a backslash.
It starts with backslash and then another backslash and that will give us see a backslash.
Just one there.
So every string that we create has a length property and length property refers to the number of characters
in that string.
So if I create a string that is just hello in all caps and I do dot length on it it tells me that's
five characters long and if I do something like my favorite number is 1 2 3 4.
That is apparently twenty seven characters long.
I won't count it but it does include a space and includes all the numbers and includes this characters
Well the colon.
So every character whatever it is it's going to be counted.
So we can retrieve the entire length of a string using the length property and we can also retrieve
individual characters like the first character the fifth character using the square bracket notation
right here.
So the way that it works we write the square brackets and then give it a number inside and that number
will correspond to the position in the string of the character that we want and Javascript starts counting
at zero.
So to get the first character let's make a string.
The Beatles.
And if I want the first character I use 0 because javascript starts keeping track at zero.
That is the first character.
And it gives me capital-T.
So if I wanted to get the capital B here I would need to do 0 1 2 3 4 and I get capital B and if I wanted
to get the very last character I could either count so 1 2 0 1 2 3 4 5 6 7 8 9 10.
And that gives me as or I could use the length property.
So I know that the Beatles that length is 11 and that's because it starts counting out one.
It's just the number of characters but the positions are kept track starting from zero.
So the last character as is always the index is always one less than the total length.
So just a side note you might be wondering why would you ever want to find the length or why would you
ever find the tenth character of a string if you can just see the whole string right here and there
are answers for that that we'll see very shortly when we cover variables