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.
Comments
Post a Comment
Leave Comment