
- TypeScript Tutorial
- TypeScript - Home
- TypeScript - Overview
- TypeScript - Environment Setup
- TypeScript - Basic Syntax
- TypeScript - Types
- TypeScript - Variables
- TypeScript - Operators
- TypeScript - Decision Making
- TypeScript - Loops
- TypeScript - Functions
- TypeScript - Numbers
- TypeScript - Strings
- TypeScript - Arrays
- TypeScript - Tuples
- TypeScript - Union
- TypeScript - Interfaces
- TypeScript - Classes
- TypeScript - Objects
- TypeScript - Namespaces
- TypeScript - Modules
- TypeScript - Ambients
- TypeScript Useful Resources
- TypeScript - Quick Guide
- TypeScript - Useful Resources
- TypeScript - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
TypeScript - Arithmetic Operators Examples
Assume the values in variables a and b are 10 and 5 respectively.
Operator | Description | Example |
---|---|---|
+ (Addition) | returns the sum of the operands | a + b is 15 |
- (Subtraction) | returns the difference of the values | a - b is 5 |
* (Multiplication) | returns the product of the values | a * b is 50 |
/ (Division) | performs a division operation and returns the quotient | a / b is 2 |
% (Modulus) | performs a division and returns the remainder | a % b is 0 |
++ (Increment) | Increments the value of the variable by one | a++ is 11 |
-- (Decrement) | Decrements the value of the variable by one | a-- is 9 |
Example
var num1:number = 10 var num2:number = 2 var res:number = 0 res = num1 + num2 console.log("Sum: "+res); res = num1 - num2; console.log("Difference: "+res) res = num1*num2 console.log("Product: "+res) res = num1/num2 console.log("Quotient: "+res) res = num1%num2 console.log("Remainder: "+res) num1++ console.log("Value of num1 after increment "+num1) num2-- console.log("Value of num2 after decrement "+num2)
On compiling, it will generate following JavaScript code.
//Generated by typescript 1.8.10 var num1 = 10; var num2 = 2; var res = 0; res = num1 + num2; console.log("Sum: " + res); res = num1 - num2; console.log("Difference: " + res); res = num1 * num2; console.log("Product: " + res); res = num1 / num2; console.log("Quotient: " + res); res = num1 % num2; console.log("Remainder: " + res); num1++; console.log("Value of num1 after increment " + num1); num2--; console.log("Value of num2 after decrement " + num2);
The output of the above program is given below −
Sum: 12 Difference: 8 Product: 20 Quotient : 5 Remainder: 0 Value of num1 after increment: 11 Value of num2 after decrement: 1
typescript_operators.htm
Advertisements