- TypeScript Basics
- TypeScript - Home
- TypeScript - Overview
- TypeScript - Environment Setup
- TypeScript - Basic Syntax
- TypeScript vs. JavaScript
- TypeScript - Features
- TypeScript - Variables
- TypeScript - let & const
- TypeScript - Operators
- TypeScript Basic Types
- TypeScript - Types
- TypeScript - Type Annotations
- TypeScript - Type Inference
- TypeScript - Numbers
- TypeScript - Strings
- TypeScript - Boolean
- TypeScript - Arrays
- TypeScript - Tuples
- TypeScript - Enums
- TypeScript - Any
- TypeScript - Never
- TypeScript - Union
- TypeScript - Literal Types
- TypeScript - Symbols
- TypeScript - null vs. undefined
- TypeScript - Type Aliases
- TypeScript Control Flow
- TypeScript - Decision Making
- TypeScript - If Statement
- TypeScript - If Else Statement
- TypeScript - Nested If Statements
- TypeScript - Switch Statement
- TypeScript - Loops
- TypeScript - For Loop
- TypeScript - While Loop
- TypeScript - Do While Loop
- TypeScript Functions
- TypeScript - Functions
- TypeScript - Function Types
- TypeScript - Optional Parameters
- TypeScript - Default Parameters
- TypeScript - Anonymous Functions
- TypeScript - Function Constructor
- TypeScript - Rest Parameter
- TypeScript - Parameter Destructuring
- TypeScript - Arrow Functions
- TypeScript Interfaces
- TypeScript - Interfaces
- TypeScript - Extending Interfaces
- TypeScript Classes and Objects
- TypeScript - Classes
- TypeScript - Objects
- TypeScript - Access Modifiers
- TypeScript - Readonly Properties
- TypeScript - Inheritance
- TypeScript - Static Methods and Properties
- TypeScript - Abstract Classes
- TypeScript - Accessors
- TypeScript - Duck-Typing
- TypeScript Advanced Types
- TypeScript - Intersection Types
- TypeScript - Type Guards
- TypeScript - Type Assertions
- TypeScript Type Manipulation
- TypeScript - Creating Types from Types
- TypeScript - Keyof Type Operator
- TypeScript - Typeof Type Operator
- TypeScript - Indexed Access Types
- TypeScript - Conditional Types
- TypeScript - Mapped Types
- TypeScript - Template Literal Types
- TypeScript Generics
- TypeScript - Generics
- TypeScript - Generic Constraints
- TypeScript - Generic Interfaces
- TypeScript - Generic Classes
- TypeScript Miscellaneous
- TypeScript - Triple-Slash Directives
- TypeScript - Namespaces
- TypeScript - Modules
- TypeScript - Ambients
- TypeScript - Decorators
- TypeScript - Type Compatibility
- TypeScript - Date Object
- TypeScript - Iterators and Generators
- TypeScript - Mixins
- TypeScript - Utility Types
- TypeScript - Boxing and Unboxing
- TypeScript - tsconfig.json
- From JavaScript To TypeScript
- TypeScript Useful Resources
- TypeScript - Quick Guide
- TypeScript - Useful Resources
- TypeScript - Discussion
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