Introduction
In this chapter, we will explore arithmetic operators in TypeScript. Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and more.
Table of Contents
- Definition
- Types of Arithmetic Operators
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Modulus (
%
) - Increment (
++
) - Decrement (
--
)
- Addition (
- Examples and Output
- Conclusion
Definition
Arithmetic operators in TypeScript are symbols used to perform mathematical operations on numeric values. They can be used with both integer and floating-point numbers.
Types of Arithmetic Operators
Addition (+
)
Definition
The addition operator adds two operands.
Syntax
let result = operand1 + operand2;
Example
This example demonstrates the use of the addition operator.
let a: number = 10;
let b: number = 5;
let addition = a + b;
console.log(addition);
Output
15
Subtraction (-
)
Definition
The subtraction operator subtracts the second operand from the first.
Syntax
let result = operand1 - operand2;
Example
This example demonstrates the use of the subtraction operator.
let a: number = 10;
let b: number = 5;
let subtraction = a - b;
console.log(subtraction);
Output
5
Multiplication (*
)
Definition
The multiplication operator multiplies two operands.
Syntax
let result = operand1 * operand2;
Example
This example demonstrates the use of the multiplication operator.
let a: number = 10;
let b: number = 5;
let multiplication = a * b;
console.log(multiplication);
Output
50
Division (/
)
Definition
The division operator divides the first operand by the second.
Syntax
let result = operand1 / operand2;
Example
This example demonstrates the use of the division operator.
let a: number = 10;
let b: number = 5;
let division = a / b;
console.log(division);
Output
2
Modulus (%
)
Definition
The modulus operator returns the remainder of the division of the first operand by the second.
Syntax
let result = operand1 % operand2;
Example
This example demonstrates the use of the modulus operator.
let a: number = 10;
let b: number = 3;
let modulus = a % b;
console.log(modulus);
Output
1
Increment (++
)
Definition
The increment operator increases the value of the operand by 1.
Syntax
operand++;
Example
This example demonstrates the use of the increment operator.
let a: number = 10;
a++;
console.log(a);
Output
11
Decrement (--
)
Definition
The decrement operator decreases the value of the operand by 1.
Syntax
operand--;
Example
This example demonstrates the use of the decrement operator.
let a: number = 10;
a--;
console.log(a);
Output
9
Examples and Output
Example 1: Using Arithmetic Operators
In this example, we will use all the arithmetic operators to perform different operations.
TypeScript Code (src/index.ts
)
let a: number = 10;
let b: number = 5;
// Addition
let addition = a + b;
console.log("Addition: " + addition); // Output: 15
// Subtraction
let subtraction = a - b;
console.log("Subtraction: " + subtraction); // Output: 5
// Multiplication
let multiplication = a * b;
console.log("Multiplication: " + multiplication); // Output: 50
// Division
let division = a / b;
console.log("Division: " + division); // Output: 2
// Modulus
let modulus = a % b;
console.log("Modulus: " + modulus); // Output: 0
// Increment
a++;
console.log("Increment: " + a); // Output: 11
// Decrement
b--;
console.log("Decrement: " + b); // Output: 4
Compiling to JavaScript
To compile the TypeScript code to JavaScript, run the TypeScript compiler:
tsc src/index.ts
Output in JavaScript (src/index.js
)
var a = 10;
var b = 5;
// Addition
var addition = a + b;
console.log("Addition: " + addition); // Output: 15
// Subtraction
var subtraction = a - b;
console.log("Subtraction: " + subtraction); // Output: 5
// Multiplication
var multiplication = a * b;
console.log("Multiplication: " + multiplication); // Output: 50
// Division
var division = a / b;
console.log("Division: " + division); // Output: 2
// Modulus
var modulus = a % b;
console.log("Modulus: " + modulus); // Output: 0
// Increment
a++;
console.log("Increment: " + a); // Output: 11
// Decrement
b--;
console.log("Decrement: " + b); // Output: 4
Running the JavaScript
To see the output of the compiled JavaScript code, run the JavaScript file using Node.js:
node src/index.js
Conclusion
In this chapter, we covered arithmetic operators in TypeScript, including addition, subtraction, multiplication, division, modulus, increment, and decrement.
Comments
Post a Comment
Leave Comment