JavaScript Basics Pt. 5 - Numbers

JavaScript
Transcript

English (Auto-generated)

and welcome back to javascript basics. We are here for part five where we're going to be going over numbers and arithmetic. So let's go ahead and get started. So numbers are another common data types in javascript, just like strings that you'll find very often and what our number is going to be used for the javascript. Well you guessed it, numeric data so there's going to be a lot of different things. You can use it for whether you need to assign a number to something like a variable or you need to perform mathematical operations. That is going to be what you'll use numbers, foreign job script and you'll see them very often. Like I said, just like strings. However, unlike strings, you don't want to place the numbers inside quotes. If you do, those numbers will simply be read as strings and I'll show you how that works in just a moment. But first off, let's go ahead and talk about forming mathematical operations in javascript. That's right. This brand new, cool, fun hobby or skill you're trying to use has math in it. Nothing more encouraging than that. But don't worry. Uh usually nothing too complicated. Uh right here is as you can see some mathematical operators that you will use in javascript. These include addition, which is the plus sign subtraction minus sign multiplication, which will be the asterisk and division, which will be the forward slash. Now let's go ahead and try these out now again, You will want to have a replica open if you don't have that open, just go ahead and head to replicate and you can either use the existing file that you used before and just delete everything that you had in your script dot Js file. Or you can go ahead and pause this for a second and head to the website, create a new html CSS and javascript project and then head into the script dot Js file. But once you've got that ready, let's go ahead and try some of that out. So let's just go ahead and do, we'll start with the council log. Is that where we can see our answer? Let's do console log two plus two. What do we think our answer is going to be? It's going to be four, just like we expected. So from here, we can also try out the different operators as well. So let's see eight minus seven. Trying to give us one, let's see here nine times nine trying to give us 81 Let's try 100 divided by 20, which is going to be using the forward slash give us five. So that is how those operators work. They were just like you would use them in math. But like I mentioned, you don't want to put these in strings. These numbers can stand completely on their own without quotation marks around them. So even if you assign them to a variable, like let's say latinum equals eight, knows that's an eight. What happens if you put quotes around it? It is now extreme. So let's say try doing this operator right here for this operation let's say eight plus eight and we want to console log are variable cr answer, you hit run. It's just gonna print out eight plus eight because you put the quotes around it to tell javascript that this is a string. So it's not gonna perform an operation between these two numbers. It's simply just going to print this out here because it assumes that this is the message you want. And what happens if you do this? It is going to print it out just like string concatenation to put the eight next to eight so I can do an operator. So right now this is this plus sign. It's not adding these numbers. It is contaminating these drinks. Another quick little lesson I want to tell you about. Oops! But these are mathematical operations here is in javascript. These numbers will be known as the operations. So this eight is an operator and and this is an operator and this plus side is known as the operator. So if you see operations mentioned or operator, I'll be talking with the operators a lot. This is what I am talking about. Alright, let's go ahead and head back on over And so we got to try these out. Let's move on to our next form of operation which is the ability to increment numbers. So in in javascript you can increment numbers Ak you can add one to a variable or a number with the plus plus operator. So that's typically going to look like this. I plus plus, you can say about that I for any number what have you. But this is probably sounding confusing because you might not have any idea what this means because unless you are used to computer programming, you may have never done anything like this before. So let's go ahead and see what that looks like and it is much easier to understand once you know what you are looking at. So let's go ahead and a new variable here and I'm using wet as you can see because this operator, the increment number operator is going to alter the value of the variable. Let's say this is a 10. So we will do X plus plus. So now we have added the increment operator to the X. And let's go and council like this X would help with I spelled console correctly and see what we get. So it's 11. So what like I mentioned before. So adding this plus plus will simply just add one to your number or whatever you've assigned to this variable here. And this is going to be something that you'll see quite often, especially once we get into functions and four loops. Um but we will get to that a little bit later. I don't want to confuse you now but I do just want to let you know that this is something common that you will see quite a bit, especially when we're getting more into the logic and program actual programming of javascript. Alright so if you can increment numbers what else can you do? Well it's a big surprise but you're never gonna guess you can deck Remittance numbers a K a do the same thing but with subtraction, so you're gonna also detriment numbers or subtract one from available or number with minus minus because you can guess let's see the exact same except that two minus signs. So let's go ahead and give that a shot. So let's say here we have uh 27 two X minus minus. We want to console log X two X is just the one. And so what do you think? Our answer will be? Well for minus in one from X and x equals seven. If you're guessing six, you're guessing right there we are. So again with the exact same as incremental numbers. Just with subtraction. And then the final lesson of this section is going to be numbers with decimal points. So you can store numbers with decimal points in javascript as well. And what you want to do is you just assign the number you want with the decimal point to the variable you want. So let me go and show you what that looks like const here. It becomes numb equals two point 3456. There is our variable with the decimal point number assigned to it. Let's go ahead console log mat. Let's see what that looks like when you print it out and there we go. So again it works exactly like regular numbers. Do you can assign them just the same to a variable now you can also do mathematical operators with the decimal points. Just like you do regular numbers. Let's go ahead and give that a shot as well. Let's say console dot log. See 2.4 plus 3.6. And you can run right over here. So we are adding these two numbers with decimal points Gives us the six. There we go. Yeah let's try it with a uh another number so you can see the result with the decimal point. There you are and it is going to work the exact same with the other operators. Let's go and try a suit too, times 3.3. So you can just like a regular math multiply or do an operation against a whole number. So you can have each one of the operations can be a whole number. One of the operations can be a decimal point number. The second record laugh and our answer is 6.6. So there we are. So again, numbers are going to be a very common data type. You will see in javascript because they are used a lot for storing values for counting things. Um and you will see a lot of these operations used later on when we get more to the logic which we are getting closer to doing. Um And there are some other operators that we will talk about that do some more slightly advanced operations but I wanted to save those for the next lessons. They don't want to overload you too much with stuff. Um I always like learning in small chunks so that's how I've been doing these videos. So I will try not to make these too long. And when we get to the point where we start building projects, I will be splitting those up to smaller portions as well because I find when I'm learning something I find those easier to digest. So that is how I will be teaching them because hopefully that works well for you and if you work better with doing more at once you can always watch more of these tutorials in a row. But there we are. And I will see you next time with some more on some more advanced operators in javascript. Alright guys, I will see you next lesson and we are getting close to functions which sounds like scary math really not as much math as you think. It's just gonna be where we will start to see logic in java script. All right, you guys have a good one. See you next lesson. Okay. No
48 Views 0 Likes 0 Comments

Getting familiar with the numbers data type and arithmetic in JavaScript

Comment
Leave a comment (supports markdown format)