🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment