🎓 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 assignment operators in TypeScript. Assignment operators are used to assign values to variables.
Table of Contents
- Definition
- Types of Assignment Operators
- Assignment (
=) - Addition Assignment (
+=) - Subtraction Assignment (
-=) - Multiplication Assignment (
*=) - Division Assignment (
/=) - Modulus Assignment (
%=)
- Assignment (
- Examples and Output
- Conclusion
Definition
Assignment operators in TypeScript are symbols used to assign values to variables. They can also be used to perform mathematical operations and assign the result to the same variable in a single step.
Types of Assignment Operators
Assignment (=)
Definition
The assignment operator assigns the value of the right operand to the left operand.
Syntax
variable = value;
Example
This example demonstrates the use of the assignment operator.
let a: number = 10;
console.log(a);
Output
10
Addition Assignment (+=)
Definition
The addition assignment operator adds the right operand to the left operand and assigns the result to the left operand.
Syntax
variable += value;
Example
This example demonstrates the use of the addition assignment operator.
let a: number = 10;
a += 5;
console.log(a);
Output
15
Subtraction Assignment (-=)
Definition
The subtraction assignment operator subtracts the right operand from the left operand and assigns the result to the left operand.
Syntax
variable -= value;
Example
This example demonstrates the use of the subtraction assignment operator.
let a: number = 10;
a -= 5;
console.log(a);
Output
5
Multiplication Assignment (*=)
Definition
The multiplication assignment operator multiplies the left operand by the right operand and assigns the result to the left operand.
Syntax
variable *= value;
Example
This example demonstrates the use of the multiplication assignment operator.
let a: number = 10;
a *= 5;
console.log(a);
Output
50
Division Assignment (/=)
Definition
The division assignment operator divides the left operand by the right operand and assigns the result to the left operand.
Syntax
variable /= value;
Example
This example demonstrates the use of the division assignment operator.
let a: number = 10;
a /= 5;
console.log(a);
Output
2
Modulus Assignment (%=)
Definition
The modulus assignment operator divides the left operand by the right operand and assigns the remainder to the left operand.
Syntax
variable %= value;
Example
This example demonstrates the use of the modulus assignment operator.
let a: number = 10;
a %= 3;
console.log(a);
Output
1
Examples and Output
Example 1: Using Assignment Operators
In this example, we will use all the assignment operators to perform different operations and assign the results.
TypeScript Code (src/index.ts)
let a: number = 10;
// Assignment
a = 20;
console.log("Assignment: " + a); // Output: 20
// Addition Assignment
a += 5;
console.log("Addition Assignment: " + a); // Output: 25
// Subtraction Assignment
a -= 10;
console.log("Subtraction Assignment: " + a); // Output: 15
// Multiplication Assignment
a *= 2;
console.log("Multiplication Assignment: " + a); // Output: 30
// Division Assignment
a /= 5;
console.log("Division Assignment: " + a); // Output: 6
// Modulus Assignment
a %= 4;
console.log("Modulus Assignment: " + a); // Output: 2
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;
// Assignment
a = 20;
console.log("Assignment: " + a); // Output: 20
// Addition Assignment
a += 5;
console.log("Addition Assignment: " + a); // Output: 25
// Subtraction Assignment
a -= 10;
console.log("Subtraction Assignment: " + a); // Output: 15
// Multiplication Assignment
a *= 2;
console.log("Multiplication Assignment: " + a); // Output: 30
// Division Assignment
a /= 5;
console.log("Division Assignment: " + a); // Output: 6
// Modulus Assignment
a %= 4;
console.log("Modulus Assignment: " + a); // Output: 2
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 assignment operators in TypeScript, including assignment, addition assignment, subtraction assignment, multiplication assignment, division assignment, and modulus assignment operators. We provided examples with their outputs to illustrate how these operators work in TypeScript.
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