Hey, everyone, in the previous video, we learned about variables, we just created our first variables
and assign some values to it.
Now we are going to learn which data types can be assigned to our variables.
So the most basic ones are strings and numbers.
So I don't have to tell you what a number is, but a string is just a piece of text like John or I'm
34 years old or the address of a website.
These are all examples of strings.
And those are the most common data types.
But we also have some very important data types that we are going to talk more about them like booleans,
undefined.
We have a race object.
Those are all very important.
And the cool thing is that this is not only apply to JavaScript.
Most programming languages, they have the same data types with some small differences, but it's pretty
much the same.
So after learning this, it's going to be very, very easy for you to learn other programming languages.
If you want.
So first, how can we know what's the data type of a variable?
So let's create one now.
Let's say var.
First name.
Let's use John.
I'm going to talk about these quotation marks in a minute.
Let me just show you something.
So I've just created this variable.
If we try to access it, we can see that the name is here.
It has been correctly assigned to our variable.
And now if we use the type of operator.
With the first name.
We can see that the console is returning strings.
So this is the data type of our first name variable.
Now let's try a number.
I'm just going to create one called age.
I'm going to assign a number.
So now I'm not using quotation marks.
Now doing the same thing type of age.
Is returning a number.
Now let's talk more in depth about strings.
Let me just refresh the page so I can clean this so we know that the text values are called strings.
So it just tried a name of a person.
We could we could type a text.
But actually, the best definition for a string is a sequence of characters.
So any string is just a sequence of characters.
The way we identify a string is by using quotation marks.
So this is why we've been using it so far.
When we send the message to the council, it has to be inside quotation marks because this is a symbol
of a string.
We can use either double quotation marks like this or single quotation marks.
Both are valid.
You just have to keep consistent.
You can start with a single and finish with a double.
This is going to return an error, but you can choose either one or the other.
So here we have a few more examples of strings.
So a passport number would be a string.
Here we have a phone number.
Although this is only.
No.
Why are we using strings?
This is because we only use numbers when we are likely to use them for calculations.
So let's say we have the age of a person.
We might use it to get the average age of a group of people.
We might use it to increment.
When the person gets older, we might use it to find their year of birth.
So there are many types of calculations that we can do with the age of a person.
But there is no calculation we can do.
We have a phone number.
So this is okay to store it as a string.
We also have the email of a person and a longer tax.
These are all examples of strings.
Now let's see a few operations that we can do with strings.
So the first one is going to be concatenation.
This is probably the most common thing that we do with strings.
I already showed you this in the previous video.
So to do this, we just use the plus sign and how this JavaScript know that it should do concatenation
and not a sum.
This is because we are using quotation marks.
So JavaScript knows that this is a string and JavaScript knows that the plus sign is used for concatenation
with strings.
If we had numbers, we would have the some of the numbers.
So if I type something like my first name and then use the plus sign and then just use an empty space.
And now the plus sign again.
And my last name, if I do this, the result is going to be a string formed by this concatenation.
As you can see in the example, we can also do it inside a variable.
So we just assign this value to the variable.
And in the end, we will have the full name.
Another interesting thing we can do with strings is getting the length and the index.
So let's create a new variable here.
So var.
First name equals John.
Now we have a string inside this variable.
And if we do first name dot lenth.
It's returning for which is the number of characters inside this string.
So in this sequence of characters, we have four characters total in each of these characters have their
own index.
So if I do it like first name.
And then I use braces like this or brackets.
And inside it, I can pass an index.
So Index says they start from zero.
So zero is the first character of a sequence.
So this is index zero.
This is index one.
Index two.
And index three.
We can get each of these characters if we want.
So if we do it like this, it's going to return J.
Which is the first character of this string.
So now it seems a bit silly.
It seems a bit useless to do this.
But trust me, we are going to use this a lot when working with strings.
Now, just one more thing that I want to show you about strings is string replace.
This is also very commonly used.
So I'm going to create a variable called You are Real.
And I'm going to use this string, which is the address of a website, you demis Web site.
So let's say I have this.
This is important with the htp ass because we can use it for a link, for example, but maybe we don't
want to show the full address if we want to send this address to an element, to a link elements.
Maybe we will send the complete address to the age Rahaf attribute.
So we go to the correct destination.
But inside the text of the link, maybe we just want to show it from here.
So if you want to remove part of a string, it's really easy to do with the replace method.
So I'm going to use my variable, which is a string.
Now I'm going to use the replace method.
So using a dot to call a method, the name of this method is replace.
And the way it works is.
Inside parentheses, first, we are going to inform the text.
We are looking to replace.
So in this case is HTP, TTP s Colline forward slash, forward slash.
Actually, I'm missing an ass here and now.
We need to inform what is going to replace this text so we can use anything we want, like an empty
string.
So when I press enter, we can see that now we just remove the age TTP s from our string.
Those are only a few examples that I wanted to show you.
If you want to learn more, you can just click here and go to W3 schools.
You are going to find many, many string methods you can use in JavaScript.
So that was all I wanted to talk about strings for the moment.
Like I said, don't worry too much.
Let's just do this step by step, little by little.
We're going to learn a little bit about each of the data types.
Then we are going to have an exercise.
And by the end of this, you will see that you will be very comfortable working with the different data
types.
So I'll see you in the next video so we can start talking about numbers.