TypeScript Arithmetic Operators
This guide examines how to use arithmetic operators in the TypeScript programming language.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide we're going to go through the arithmetic operators in typescript. Now if typescript is not your first programming language then this may seem like a bitter review for you because these operators are relatively standard across all programming languages. However it's still a good idea to see the exact syntax to use and if you're coming from a language like Ruby then these last two operators the Incrementor are in the Decrementor do not exist in those languages so it's important if you are coming from a language that does have these to see exactly how they work and in Ruby we have to usually build some type of work around to mimic this type of behavior. So that's going to be helpful and we'll do those at the end.

So to start off I'm going to declare two variables so I'm going to create a variable called numOne and I'm going to make this of type number and set the value equal to 1. And then I'm going to duplicate this and create another number called numTwo and set the value equal to 2 keep the numbers nice and small just to make it easy. And now I'm going to for the sake of using this as a reference in your source code all this is going to be pushed to github. And so I'm going to add some comments just so you can see the way that it works.

var numOne : number = 1;
var numTwo : number = 2;

Addition

So you start off by saying addition and this way you have some output in the terminal that will help you understand it. And here they dish an operator is just a standard plus symbol. Something Say numOne plus numTwo and that is going to be everything that we need for addition just to make sure that everything is working properly I am going to run this program and now I'm going to run a arithmetic operators and you can see that that worked perfectly. So we're good on that side.

console.log(“Addition:”);
console.log(numOne + numTwo);

Subtraction

Now I'm going to copy this. And now let's do subtraction. Subtractions pre-standard it's just going to be the minus symbol and I'm not going to run the program after each symbol just for the sake of brevity and because a lot of these are pretty straightforward.

console.log(“Subtraction:”);
console.log(numOne – numTwo);

Multiplication

Multiplication uses the star or Asterix symbol. And we can also even change so if we want to change these values just so you don't have to multiply by one we can just change numOne and make this equal to 10. And we can change numTwo, set this one equal to 15. Just so we have switch it up and get a little bit different behavior. So that's multiplication.

console.log(“Multiplication:”);
numOne = 10;
numTwo = 15;
console.log(numOne * numTwo);

Division

Now on next one would be natural to go to would be division. Will take numOne divided by numTwo. And because we reassign these values this is actually going to be 10 divided by 15. So that's division.

console.log(“Division:”);
console.log(numOne / numTwo);

Modulus

Now we're going to get one that if you haven't used it a lot may seem a little bit trickier. And this one is called the modulo or modulus operator this is what you use usually when you want to see if a number is odd or even. But what it does is it lets you know if there's a remainder. So I'm going to declare a few more variables here just so you can see exactly how it works. So I'm going to say numThree and set this equal to a number I’m going to set equal to two and then I'm going to declare another variable called numFour and we'll set this one equal to 20. And what we're going to do is test to see if 20 is odd or even. So for this syntax something I do for use the modulus operator which is also the percent symbol and then run that.

console.log(“Modulus:”);
var numThree : number = 2;
var numFour : number = 21;
console.log(numFour % numThree);

Incrementor

Next let me. Hold this up. So this next one is going to be a little bit trickier. This is going to be the incrementor and it's the incrementor if you just try to put this inside of the console log statement. This will not work. So this adding in. So if I tried to do number one and then increment this this is not going to work because console log is simply looking at the value of numOne. So instead what I'm going to do here is I'm going to get rid of the console log statement completely. And we're going to create a small loop here. So I'm going to create a loop from start with var x set it equal to the data type number and then I'm going to set that equal to zero. And now I'm going to create a very basic while loop and don't worry just because we haven't gotten into while loops yet. So I'm going to go with a very basic one. All I’m going to do is I'm going to say well X is less than 10. I want you first to console log out the value of x and then after that I want you to increment X. So this is something that's pretty standard that you'll see while using while loops in typescript JavaScript and a lot of other programming languages that use the while loop. This is going to increment this value. And when we run the program you'll see you'll start at zero. Then Ill add up to 1 and then loop around again then it will print out the one and it will add and increment it to two and keep on going down the line like that.

console.log(“Incrementor:”);
var x : number = 0;

while (x < 10) {
  console.log(x);
  x++;
}

Decrementor

Now let's use the Decrementor and it is going to do pretty much exactly like what it sounds it's going to do it's going to detriment the value by one with each time. This time I'm going to start off with at 10 and I'm going to say well x is greater than zero. I want you to print this out. And here I want you to decrement this make sure to fix that because if you didn't you'd end up with an infinite loop. OK this all looks good. It's quite a bit of code since we ran it. So let's make sure that everything is working. I'm going to compile the code. Everything looks good there. And then let's run this and everything worked.

console.log(“Decrementor:”);
var x : number = 10;

while (x > 0) {
  console.log(x);
  x--;
}

So we go line by line here we can see we have addition which added subtraction subtracted multiplication, division. This is a key one right here the modulus operator gave a value of zero which means it worked because what. Remember we were trying to determine if 20 was even or not. And a trick on how to do that in programming. Is to be able to use this modulus operator because what it does is it takes the value of the left and right and then it tells you if when you divide three into four or you know when you divide the value on the right hand side by the value on the left side it tells you if there's a remainder. And if you set the value of three to two it means that this is going to tell you if the value on the left is a even or odd number because if it's an even number there the remainder is always going to be zero. If it's an odd number so let's say that it changes to 21 and we rerun this code. You can see that the modulus value is now set to 1, which would tell us that this is odd. This is something that is very common in programming languages to be able to determine if a number is odd or even. So that's the modulus side. Now the incrementor the decrementor Let's see how those work. You can see with incrementor it did exactly like we expected it printed out 0 and then went all the way until it was 9 and incremented by 1 with each time. And then with the decrementor it started on 10 and went all the way down to zero.

So all of this works I'm going to push all this code up to github and you'll have access to it in the show notes and you'll be able to now use this in reference if you ever have questions about the syntax for arithmetic operators in typescript.

Resources