Hey, everyone.
Welcome back.
Continuing with the data types, we're now going to talk more about numbers before we get started.
I just want to say something quickly.
I'm just using the console to write the examples just so it's faster, just so I don't have to go back
and forth to Visual Studio Code and then back here.
But I encourage you to write all the examples inside the JSC file.
So you have everything there.
Of course, any time you want, you can come back here to the written material and check all the examples
again.
This is why I made this material.
So having said that, let's move on.
We know that we can assign numbers to variables.
All we need to do is use only numbers and don't use quotation marks.
So if we include quotation marks, JavaScript is going to treat this as a string.
So like this, JavaScript knows we are working with a number.
So we have integers.
And we also have numbers with decimal places like this.
We should always use DOT.
So never use comma.
And we also cannot use thousand separators.
So if I have a price variable.
So var price, let's say the price is more than a thousand.
I can't do it like this.
This is going to confuse JavaScript.
And this is going to return an error.
We never use thousand separator.
So this is a thousand.
This is ten thousand.
This is how we write numbers.
And if you have decimal places you should use DOT.
So this is the right way of writing a number.
We also have big numbers in JavaScript so we can represent them with this exponential notation.
So we don't have a very, very long number.
This number here means two times 10.
By the power of 45.
So now let's check some calculations that we can do with numbers.
So, of course, we have the basic math operations when dealing with programs.
We're going to do calculations all the time.
So we need at least the basic math calculations.
So I can have a variable.
Called Numb one.
Actually, I just added an empty space here.
So now it's right, so numb.
One.
I'm also going to create another variable num, too.
And now we can make any kind of calculations we want.
So some is just going to be number one plus.
Numb, too, as you can see, we have the calculation done here.
We can also do subtraction.
We have a dash division.
We have a forward slash and multiplication with an asterisk.
Just one thing that you might be asking yourself.
Why was I using console dot log in the code?
And now I'm not doing this anymore.
I'm just typing in the operations directly.
If you're in the console here, you can just type it directly like this.
But this is not going to work in the JavaScript file.
In the JavaScript file.
If you want to send anything to the console, you need to write, console, dot log.
So moving on, we can also use parentheses to form expressions.
So if we want the average of these two numbers, you should just write it like this.
So num one plus.
Numb, too, divided by two.
This is not going to be correct because following the hierarchy of math operations, it is going to
do the division first and then it's going to do the some.
So in order to sum the numbers first, we just enclose them inside parentheses like this.
And then we do the division.
So now we have the correct average.
Let me clear this real quick.
And now let's talk about the math object, if we want to go further with calculations, maybe we want
more than just the basic math operations.
We have a built in object in JavaScript.
It is called math.
We have a capital M so we can use it to make some more complex stuff like two to the power of four.
So how can we do this?
We can just type math and then any method of the math object can be access.
We have a dot when you do this.
You have all the lists.
So the console brings all the list of methods that we can use with the math object.
So if we type Powell.
So now two to the power of four is just two karma, four to now we have the result, which is 16.
We can also round numbers so we can use the round method.
And this is going to just round to the nearest integer so I can do math.
That round.
And then if I'm trying to round two point seven, it is going to round two three.
If I'm trying to round two point three, it is going to round two to.
We can also round two, the current integer, so math dot floor.
It doesn't matter if I have nine here.
It is always going to round to the low integer and the opposite would be the CEO.
Method.
So this time it is going to round to the bigger integer.
So even if I have to point to, it's still going to round two, three.
Sometimes it's useful to do this.
Now we also have the square root.
And another very interesting thing we have with the math object.
We programmers use this a lot.
Is the random method.
So if you do it like this, we always need parentheses for methods.
We're going to talk more about this even if we don't pass anything inside.
We should always use parentheses to run methods.
So now we are calling the random method.
And what it does is generating a completely random number between zero and one.
So if I do this one time, it comes one number.
If I do this another time.
Now it's a different number.
Every time we do it, it's going to be a completely different number.
So now how can we have a random number between zero and 100?
For example.
Wow, it's easy.
We can just do math.
Dot random.
Then we multiplied by 100.
So the smallest one, which is zero, is going to be zero.
And the biggest one, which would be zero point ninety nine, is going to be close to 100.
So this way we have a random number from zero to 100.
And now if we want to around it, we can just use the math that round.
And now every time we run this, we are going to have a random number from zero to 100.
Pretty cool right now besides the math object.
We also have some methods that can be used with the no data type.
So one example is the two fixed method.
So wi fi, have a number.
Let me just clear this real quick.
I'm going to create a variable called my.
The number equals.
Three point something just like this.
Now, if I use the round function from the math object, it is just going to round it to four.
I could use the floor or the CEO methods, but still, it's not exactly what I want because maybe I
just want to round this number to two decimal places.
So in order to do this, we can call the variable my number.
So now it's just a different way of writing it.
So we just use the name of the variable and then a dot and then we can write to fix it, which is the
name of this method.
And inside here, we just informed the number of decimal places we want you if we don't write anything
here is just going to round to no decimal places.
If we write to, then it's going to round to two decimal places.
Three, four.
We can choose anything we want here.
So at this point, you might be getting a bit confused because with the math object, we just wrote
it like this.
So math dot round.
And then we place the number inside.
So in this example, we would just write it like this.
So math dot round.
And then inside it.
We are going to put our variable, so my number.
So we would write it like this.
And this is a different way of writing it.
So my suggestion to you is that you don't worry about this at the moment because all of these things
are going to be learned naturally.
You don't have to understand everything.
Why this one?
You write it like this.
Why this one?
You write it like that.
The intention here is just to get started getting comfortable with JavaScript writing code.
And then you see that you won't have any trouble understanding these things.
All right.
So moving on now.
We are going to talk about incremental operators, but this video is getting a bit too long.
So I'm going to stop it here and we're going to continue in the next video.